Gadgets

Thursday, 21 January 2016

Explain about ArrayList with an example program

1. ArrayList allow Hetrogeneous data elements.
2. Underlying Data Structure is Resizable Array or Growable Array.
3. ArrayList can hold object types only.
      ArrayList al=new ArrayList();
      al.add(10); // allow integer
      al.add(“A”); //allow string
4. Null insertion is possible.
5. Duplicates are allowed.
import java.util.*;
public class ArrayListDemo  {
               public static void main(String[] arg)  {                
                               ArrayList l=new ArrayList();
                               l.add("n");
                               l.add("a"); //char insertion
                               l.add(12); //int insertion
                               l.add("r");
                               l.add("e");
                               l.add(14);
                               l.add("n"); //duplicate insertion
                               l.add(null); //null insertion
                               System.out.println(l); //[n, a, 12, r, e, 14, n, null]
                               l.remove(2); //remove l[2]
                               System.out.println(l); //[n, a, r, e, 14, n, null]
                               l.add(2,10); //add l[2]=10
                               System.out.println(l); //[n, a, 10, r, e, 14, n, null]
               }             
}


No comments:

Post a Comment