1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* Sort elements of Java ArrayList Example This Java Example shows how to sort the elements of java ArrayList object using Collections.sort method. */ import java.util.ArrayList; import java.util.Collections; public class SortJavaArrayListExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("1"); arrayList.add("3"); arrayList.add("5"); arrayList.add("2"); arrayList.add("4"); /* To sort an ArrayList object, use Collection.sort method. This is a static method. It sorts an ArrayList object's elements into ascending order. */ Collections.sort(arrayList); //display elements of ArrayList System.out.println("ArrayList elements after sorting in ascending order : "); for(int i=0; i<arrayList.size(); i++) System.out.println(arrayList.get(i)); } } /* Output would be ArrayList elements after sorting in ascending order : 1 2 3 4 5 */ |
Good Tutorial Collection..
i have never ever seen before like this
this is great man:)do you have a good book of java for absolute beginers?thanx
This program helped me a of and helped me in thinking for a solution in a new best way thank you.
me: Shamiem Ahmad Mir.
When I try to apply the sort method to the Collections, I get the error:
Bound mismatch: The generic method sort(List<t>) of type Collections is not applicable for the arguments (ArrayList<mvccontact>). The inferred type MVCContact is not a valid substitute for the bounded parameter <t extends=”” comparable<?=”” super=”” t=””>>
what can I do?
u need to implements Comparable?
sort method working (Observer dp it mean u got final algorithm of comapreTo and swap u need to give the comapreTo by implements Comparable)
This really helped. Thank you!