ArrayList in Java

  • Arraylist class implements List interface.
  • You can say compare Arraylist as dynamic Array.
  • ArrayList is a resizable-array implementation of the List interface.
  • It implements all optional list operations, and permits all elements, including null.

Code Example:


import java.util.*;

public class ArrayListExample {
   public static void main(String args[]) {

	  ArrayList obj = new ArrayList();

	  //Add element at the the array list
	  obj.add("Ajeet");
	  obj.add("Harry");
	  obj.add("Chaitanya");
	  obj.add("Steve");
	  obj.add("Anuj");

	  //Displaying array list elements
	  System.out.println("Current array list is:"+ obj);

	  //Add element at the given index
	  obj.add(0, "Rahul");
	  obj.add(1, "Justin");

	  //Remove elements from array list like this
	  obj.remove("Chaitanya");
	  obj.remove("Harry");

	  //Remove element from the given index
	  obj.remove(1);
   }
}
Advertisements

Leave a Reply