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)); } }
2 ответов
+ 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
0
Thanks for all, but I need all program.