What is ArrayList ?

It is a dynamic arraythat can grow as needed.standard arraycan not grow or shrink later, which means that you must know in advance how many elements array will hold. But sometimes you may not know the size of array before run time. In this situation we need ArrayList.
ArrayList extends AbstractList and implements List interface.
Example:
import java.util.*;
class demo
{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add("bhabani");
al.add("pinku");
al.add("mahesh");
al.add("arvind");
al.add("arjan");
System.out.println("size of array list is:"+al.size());
System.out.println(al);//to display the list
al.remove(2);//to remove an element
//al.remove("mahesh");//same as above line
System.out.println(al);
System.out.println(al.contains("bhabani"));//whether one element exist
al.removeAll(al);//to remove all elements
System.out.println(al);
System.out.println(al.isEmpty());//whether the list is empty
}};

No comments:

Post a Comment