How to sort the elements of a list ?

You could use a tree set or Collections.sort().
Using Collection.sort():
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList ts=new ArrayList();
ts.add(7);
ts.add(3);
ts.add(1);
ts.add(10);
ts.add(9);
Collections.sort(ts);
System.out.println(ts);
}};

Using TreeSet
import java.util.*;
class demo
{
public static void main(String[] args){
TreeSet ts=new TreeSet();
ts.add(7);
ts.add(3);
ts.add(1);
ts.add(10);
ts.add(9);
System.out.println(ts);
}};

No comments:

Post a Comment