Artikel Terbaru

Saturday, 3 December 2011

Sorting List of Numbers

In a java programming, Let us say We have a set of numbers say 100,200,4,5,79,26. In java, we can use either Array or List implementation(ArrayList or LinkedList in java.util package

.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class SortDemo {
public static void main(String args[]){
 Integer[] numbs=new Integer[]{100,200,4,5,79,26};
 System.out.println("Initial List="+numbs);
 /*For sorting numbers we can use Arrays's sort method*/
 Arrays.sort(numbs);
 
 Integer[] numbs1=new Integer[]{100,200,4,5,79,26,20};

 /*For sorting numbers we can also use List's  sort method*/
 List listNumbers=Arrays.asList(numbs1);
 Collections.sort(listNumbers);
 System.out.println("Sort List using List sort="+listNumbers);
 }
}

Arrays can store the static list of data items, that means size cannot be grown and fixed.where as List store the list of dynamic data itmes.