ArrayList in Java

Introduction to ArrayList in JAVA

ArrayList is used to create dynamic arrays in Java programming. In the implementation of ArrayList class, this class extends the AbstractList and implement and interface named list interface. This type of arrays is like linked list, it grows and shrinks as the data increases and decreases in the array. Previously we have discussed simple arrays in Java. In this tutorial, you will learn how to use ArrayList in Java.

In normal routine of Java, the arrays are of fixed size and we have to define the size before using an array. While we implement them once we create an array with some size we have no option to resize it so you must have future knowledge that what is the expected length and size of the array.

Advantages of ArrayList

As we stated in the previous tutorial about standard array, they have fixed size, but when in java we use ArrayList it will enable us to add or remove element with the reduction of array size this means that when you add an element in array, the size of array automatically increases and when you remove an element the size will decrease.

Constructors in ArrayList

In Java array list there are three possible types of the constructors.

  • ArrayList()
  • ArrayList(Collection object)
  • ArrayList(int size)

ArrayList( ) Simple constructor with no parameters is used to create an empty array.

ArrayList(Collection object) This array list is by default initialize with elements of the given collection.

ArrayList(int size) This constructor creates an array of given size in the parameters.

Creating ArrayList

If you want to create an ArrayList you must create an instance/object of ArrayList class, and define the type of the array in triangular brackets.  You can also create an ArrayList without specifying the type of ArrayList.

Syntax

ArrayList<type of list> list name=new ArrayList<type of list>();

Example

Adding Elements in ArrayList

When you want to add elements to an ArrayList add() method is used which takes parameters of the same type as specified at the time of declaring ArrayList.

Displaying complete ArrayList

When you want to display the complete ArrayList you can display it through the instance you created of the ArrayList class.

Displaying some specific data of ArrayList

You can also display data at some specific location in ArrayList all you have to know is the location of the data as in the case of Arrays. get() method is used to get the data at some specific index.

Removing Elements from array

you can also remove elements from an ArrayList, remove() method is used to remove elements from the ArrayList in Java. You can also pass the index of the value you want to delete in the remove() method.

Important ArrayList Functions

  1. Array.add(value);
  2. Array.add(index,value);
  3. Array.remove(value);
  4. Array.remove(index);
  5. Array.size();
  6. Array.clear();
  7. Array.clone();

All the above function are defined by their names and do as their names are like the add function is used to add elements in array and remove is use to remove the size function return the actual size of the array and clear function remove all elements from array the clone function make and return the copy of that array.

Read more about ArrayLists in Java