Pages

Friday 6 March 2015

JAVA QUESTIONS

Differencre between ArrayList and Vector List?
·         ArrayList and Vector both having same data structure internally, which is Array
·         Vector is by default synchronized, means at a time only one thread can access its methods from out side, where as ArrayList is non-synchronized means N number of threads can access at a time
·         But we can make ArrayList as synchronized by using Collections class, see it bellow
·         Both Vector and ArrayList  have capability to re-size dynamically, means Vector will Doubles the size of its array when its size increased, but ArrayList increased by Half only
How to Sort a Array List in Java?
import java.util.ArrayList;
import java.util.Collections;

public class SortingArrayList {
     public static void main(String args[]){
        ArrayList<String> al = new ArrayList<String> ();
             al.add("sai");
            al.add("sri");
            al.add("arun");
            al.add("john");
            al.add("babu");
            al.add("athish");
             System.out.println("Printing List before sorting......");
             for(String value: al){
                 System.out.println(value);
             }   Collections.sort(al);
              System.out.println("Printing List after sorting......");
             for(String value: al){
                System.out.println(value);
             }
}

}

No comments:

Post a Comment