60 709
Assignments Done
Successfully Done
In April 2018

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 = "";
int watched = 0, mostWatched = 0;
for (int i = 0; i < total; i++)
watched = mList[i].getWatched();
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
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

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

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

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

[Karl Ng, Tom Rich, Jeff Smith, John Smith]

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

Privacy policy Terms and Conditions