Java Collections

  • The Java API provides special classes to store & manipulate group of objects.
  • One such class is ArrayList. Standard java array are of fixed length , which means that after they are created , they cannot expand or shrink.
  • On otherhand, ArrayList are created with an initial size, but when this size exceeded, the collection is automatically enlarged.
  • When Object removes, the ArrayList may shrink in size.
  • Note that the ArrayList classs is in java.util package so its necessary to import it before using it.
  • Example-
    import java.util.ArrayList;
    ArrayList color = new ArrayList();
  • We can optionally specify a capacity & type of objects the arrayList will hold.
  • ArrayList<String> color = new ArrayList<String>(10) ;
  • This codeĀ  above defines an ArrayList of Strings with 10 as initial size.
  • E.g ArrayList<Integer> a = new ArrayList<Integer>(9);
  • The ArrayList class provides a number of useful methods for manipulating its objects.
  • The add() method adds new objects to the ArrayList.
  • remove() method remove object from the ArrayList.
  • import java.util.ArrayList;
    public class Myclass{
          public static void main(String args[]){
              ArrayList<String> color = new ArrayList<String>();
              color.add("Red");
              color.add("Green");
              color.add("Blue");
              color.remove("Green");
              System.out.println(color);
          }
    }
    output-
    [Red , Blue]

Other Methods-

  • contains() – Returns true if the list contains the specified elements.
import java.util.ArrayList; 
public class Myclass{ 
     public static void main(String args[]){ 
         ArrayList<String> color = new ArrayList<String>(); 
         color.add("Red"); 
         color.add("Green"); 
         color.add("Blue"); 
         color.add("REd");            //Red and REd are two different obj 
         color.remove("Green"); 
         System.out.println(color.contains("Red")); 
         System.out.println(color.contains("Green"));
     } 
}
output -
true
false
  • get(index) – Returns the element at the specified position in the list.(index is integer value)
import java.util.ArrayList; 
public class Myclass{ 
     public static void main(String args[]){ 
          ArrayList<String> color = new ArrayList<String>(); 
          color.add("Red"); 
          color.add("Green"); 
          color.add("Blue"); 
          color.remove("Green"); 
          System.out.println(color.get(1)); 
          System.out.println(color.get(0));
    } 
}

output-
Blue
Red
  • size() – Returns the number of elements in the list.
import java.util.ArrayList; 
public class Myclass{ 
     public static void main(String args[]){ 
          ArrayList<String> color = new ArrayList<String>(); 
          color.add("Red"); 
          color.add("Green"); 
          color.add("Blue"); 
          System.out.println(color.size()); 
     } 
}
output-
3
  • clear()Removes all of theĀ  elements from the list.