Thursday 13 February 2014

Playing with List

Confusion which I have first time and require attention :
  1. Sometimes we confused about List that it is a class and try to create a Object of it.(First time I did this :) ). After that I realize that List is a interface.
  2. Use generic types whenever using any collection like List. Why ? According to Java doc : Generics add stability to your code by making more of your bugs detectable at compile time. (I was not aware of this first time.)

   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
   import java.util.ListIterator;


   public class TestClass implements List<String>{
       public static void main(String ...strings){
           List<String> list = new ArrayList<String>();
       list.add(null);      null is allowed in List.
       list.add("Ram");
       list.add("Ram"); duplicate elements are allowed in List.
       list.add(1,"Shyam"); inserts "Shyam" at position 1 and also it change index of all elements after that index.
          list.get(0);
     System.out.println(list);
     System.out.println(list.size());
   
    }
}

No comments:

Post a Comment