Home » Java » ArrayList in java

ArrayList in java

ArrayList in Java :

ArrayList is subclass of List interface.ArrayList in Java is most frequently used collection class because of the flexibility it offers.The ArrayList class is an implementation of the List interface that allows us to create resizable-arrays.Most of the developers choose ArrayList over Array to store elements.

The main issue with Array is that length of Array is fixed, so if it is full we cannot add more elements to it, also if you are removing elements from Array memory consumption will be same as it will not shrink. At the same time ArrayList can dynamically grow and shrink as per the requirement.

Since we can not modify size of an array after creating it, we prefer to use ArrayList in Java which re-size itself automatically once it gets full.

ArrayList Overview:

The ArrayList class uses an array to store the objects. As a result the access is very fast because it can internally use the array index to locate an object.

arraylist


At the same time the insertions and deletions are very slow. The reason for this is when we insert an element in ArrayList apart from the end position It has to reshuffle the entire Array.That is why ArrayList is not a great data structure to use if you are doing too many insertions and deletions on a Collection.

Capacity of ArrayList

By default the initial capacity of an ArrayList is 10.So when we create an ArrayList, if we don't specify a capacity by default it will create an array of size 10.If we store 10 objects and now we are trying to store the 11th object, if we go beyond the capacity,the ArrayList will create another array of one and half times the size of the initial array. And it will copy entire elements or objects into the new array.

So it's always a good practice, when we create an ArrayList we can pass the initial capacity if we know how many objects we are going to be stored into that ArrayList.

When to use an ArrayList?

If we are doing a lot of read operations compared to inserts or deletes in our application then we should be using an ArrayList, because reading is very fast.Most of the real time applications are designed that way.That is, they read more than inserting and deleting into a Collection.

Characteristic of ArrayList

  • ArrayList can contain duplicate elements.
  • ArrayList will maintain insertion order.
  • Array list objects are not synchronized by default.
  • Random access of elements because ArrayList works at the index basis.

Example of Java ArrayList class :


package com.example;

import java.util.ArrayList;

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

		ArrayList<String> list = new ArrayList<String>();

		/* Add some elements to the array list */
		list.add("Vicky");
		list.add("Raj");
		list.add("Anshu");
		list.add("Sanu");
		list.add("Bala");

		/* Displaying array list elements */
		System.out.println("Elements of ArrayList :" + list);

		/* Add element at the given index */
		list.add(0, "Mukesh");
		list.add(1, "Bablu");

		/* Remove elements from array list like this */
		list.remove("Bala");
		list.remove("Raj");

		System.out.println("Now Current element is:" + list);

		/* Remove element from the given index */
		list.remove(2);

		System.out.println("Now Current element is:" + list);
	}
}

Output

Elements of ArrayList :[Vicky, Raj, Anshu, Sanu, Bala]
Now Current element is:[Mukesh, Bablu, Vicky, Anshu, Sanu]
Now Current element is:[Mukesh, Bablu, Anshu, Sanu]

Methods of ArrayList class :

In the above example we have used methods such as add and remove. There are number of methods available which can be used directly using object of ArrayList class. Some of the important method of ArrayList is given below.

1) add( Object obj): This method adds an object obj to the ArrayList.

obj.add("hello");

This statement would add a string hello in the arraylist at last position.

2) add(int index, Object obj): This method adds an object obj to the given index of ArrayList.

obj.add(2, "Java");

code mentioned above will add the string java to the 2nd index (3rd position as the array list starts with index 0) of array list.

3) remove(Object o): This method Removes the object obj from the ArrayList.

obj.remove("Mukesh");

code mentioned above will remove the string "Mukesh" from the ArrayList.

4) remove(int index): This method removes element from a given index.

obj.remove(3);

Code mentioned above will remove the element of index 3 (4th element of the list).

5) set(int index, Object obj): This method will be used for updating the element in ArrayList.It replaces the element present at the specified index with the object obj.

obj.set(2, "Ravi");

Code mentioned above will replace the 3rd element (index =2 is 3rd element) with the value Ravi.

6) int indexOf(Object obj): This method will return the index of the object obj. If the element is not found in the list then this method will return the value -1.

int pos = obj.indexOf("Ravi");

Code mentioned above will return the index (position) of the string "Ravi" in the list.

7) Object get(int index):This method will returns the object of list which is present at the specified index.

String str = obj.get(2);

Code mentioned above will return the string stored at 3rd position (index 2) and would be assigned to the string "str".

8) int size():This method will return size of the ArrayList(Number of elements of the list).

int number = obj.size();

9) boolean contains(Object obj):This method will check whether the given object obj is present in the array list.If object is there, it will return true else it will return false.

obj.contains("Mukesh");

Above code will return true if the string "Mukesh" is present in the list else it will return false.

10) clear():This method is used for removing all the elements of ArrayList. The below code will remove all the elements of ArrayList whose object is obj.

obj.clear();


Previous Next Article

comments powered by Disqus