Answer to Question #16282 in Java | JSP | JSF for Barry

Question #16282
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 = &quot;&quot;;
int watched = 0, mostWatched = 0;
for (int i = 0; i &lt; total; i++)
watched = mList[i].getWatched();
mostWatched = watched;
print = &quot;Most watched is &quot;+mList[i].getTitle()+&quot; watched &quot;+mostWatched+&quot; times&quot;;
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&#039;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
Expert's answer
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


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")
& };

& List<Name> names = Arrays.asList(nameArray);
& Collections.sort(names);
& System.out.println(names);

If you run this program, here's what it prints.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog