Hi, i am pretty much a beginner programmer. I need to list out array items according to their max value down. It is a movie list where i need to list the movies according to the amount of views. I can calculate the one movie that has the most views with the following code.
public void mostWatchedMovies(Movie [] mList)
{
String print = "";
int watched = 0, mostWatched = 0;
for (int i = 0; i < total; i++)
{
watched = mList[i].getWatched();
if(watched>mostWatched)
{
mostWatched = watched;
}
print = "Most watched is "+mList[i].getTitle()+" watched "+mostWatched+" times";
}
JOptionPane.showMessageDialog(null, print);
}
The array is called mList, the getTitle and the getWatched are simply returning movie title and amount of views from a movie class.This works fine. I'm completely unfamiliar with arraylist and its sort methods so i want to do this with a normal array of movie objects. Could i use this most watched calculated in maybe for loop to countdown from most watched
1
Expert's answer
2012-10-11T11:17:49-0400
You can use the design ArrayList<mList> it is a dynamic array& of methods for element (object).get(int index) and recording element (object).add(el mList);Also you can use the mechanism forch of cycles for ArrayList.You can also sort the array Example forch ArrayList<mList> m=new ArrayList<mList>(); for(mList n:m){ } Example sort
Just to show that it all works, here's a program that builds a list of names and sorts them.
import java.util.*;
public class NameSort { public static void main(String[] args) { & Name nameArray[] = { new Name("John", "Smith"), new Name("Karl", "Ng"), new Name("Jeff", "Smith"), new Name("Tom", "Rich") & };
Comments
Leave a comment