Hi! I need to sort an ArrayList, but I do not know how I can resolve it. Please, help me!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi! I need to sort an ArrayList, but I do not know how I can resolve it. Please, help me!!

Code program: import java.util.ArrayList; public class Ex8 { public static void main(String[] args) { ArrayList list=new ArrayList(); list.add(1); list.add(9); list.add(4); list.add(-2); int i, j, aux; for(i=0; i<list.size(); i++) { for(j=i+1; j<=list.size(); j++) { if(list.get(i)>list.get(j))// heare is an eror: "The left-hand side of an assignement must be a variable. ................... } } for(i=0; i<list.size(); i++) { System.out.println(lista.get(i)); } }

14th Aug 2016, 11:31 AM
Paula Ariton
Paula Ariton - avatar
2 Answers
+ 1
So, when you created your array list, you didn't wrap a type in its declaration, so its going to take all inputs as objects. Since you can't quite compare if an object is greater then another(Console actually let me bypass, but it's still better practice), you are getting an error. Just do ArrayList<Integer> list = new ArrayList(); Also, get rid of = in j<= otherwise out of bounds error. Finally. Last println has lista, should be just list. Fix all that, and it'll compile. But won't sort. Easy sort, can be done with import java.util.Arrays; Just type: Arrays.sort(list); boom, thuper thimple
14th Aug 2016, 3:33 PM
James
James - avatar
0
Thanks for all, but I need all program.
14th Aug 2016, 4:15 PM
Paula Ariton
Paula Ariton - avatar