An ArrayList in Java is a dynamic and resizable array declared in java.util package. Unlike standard array whose size is fixed, the ArrayList contents can dynamically grow and shrink. Following is a simple example which includes common Arraylist methods:
import java.util.ArrayList;
import java.util.Iterator;
public class CommonCollectionAPIsTest {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList();
al.add(11);
al.add(22);
System.out.println("Value at index 0 " + al.get(0));
System.out.println("Array list size: " + al.size());
System.out.println("Array list is empty: " + al.isEmpty());
Iterator<Integer> iterator = al.iterator();
System.out.println("Iterator values:");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
al.remove(0);
System.out.println(al);
}
}
No comments:
Post a Comment