Confusion which I have first time and require attention :
- 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.
- 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