Arraylist overview

ArrayList is a java class which implements List interface

Main features of ArrayList


1) It uses indexing to store the elements sequentially

2) It allows duplicate elements

3) It is not synchronized

4) Elements from Arraylist can be accessed using index

5) We can also use iterator and ListIterator to access the elements

6) Elements in ArrayList can grow and shrink dynamically

We can create ArrayList using its constructor as below

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


Example :
If we want to store the list of programming language names, we can use ArrayList as below

  1. package com.kb.collectionsEx;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Vector;
  5. public class ArrayList1 {
  6.     public static void main(String[] args) {
  7.     ArrayList<String> list = new ArrayList<String>();
  8.     list.add(“Java”);
  9.         list.add(“c++);
  10.         list.add(“python”);
  11.         list.add(“Java”);
  12.     System.out.println(list);
  13.     }
  14. }
package com.kb.collectionsEx;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class ArrayList1 {
	public static void main(String[] args) {
	ArrayList<String> list = new ArrayList<String>();
	list.add(“Java”);
        list.add(“c++”);
        list.add(“python”);
        list.add(“Java”);
	System.out.println(list);
	}
}
Note :
We can see that, duplicate elements are allowed and also insertion order is maintained.


Some of the important methods of ArrayList

1) add( Object o) :

This method adds the object to the arraylist.

Example :

  1. ArrayList<String>  obj = new ArrayList<String>();
  2. obj.add("java");
ArrayList<String>  obj = new ArrayList<String>();
obj.add("java");

This statement adds a string “java” in the arraylist at the end

2) add(int index, Object o):

This method adds the object to the array list at the given index.

  1. obj.add(2, "java");
obj.add(2, "java");

It 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) :

Removes the object from the ArrayList.

  1. obj.remove("bad things");
obj.remove("bad things");

This statement will remove the string “bad things” from the ArrayList.

4) remove(int index) :

Removes element from a given index.

  1. obj.remove(2);
obj.remove(2);

It removes the element of index 2 (3rd element of the list as List starts with o).

5) set(int index, Object o) :

Used for updating an element

It replaces the element present at the specified index with the specified object.

  1. obj.set(2, "java");
obj.set(2, "java");

It replaces the 3rd element (index =2 is 3rd element) with the value “java”.

6) int indexOf(Object o) :

It gives the index of the given object

If the element is not found in the list then this method returns the value -1.

  1. int pos = obj.indexOf("java");
int pos = obj.indexOf("java");

This gives the index (position) of the string “java” in the list.

7) Object get(int index) :

It returns the object of list which is present at the specified index.

  1. String str= obj.get(2);
String str= obj.get(2);

This returns the string object stored at 3rd position (index 2).

If we have integer array list then the returned value should be stored in an integer variable.

8) int size() :

It gives the size of the ArrayList

In other words, returns the number of elements in the list.

  1. int numberofitems = obj.size();
int numberofitems = obj.size();

9) boolean contains(Object o) :

It checks whether the given object is present in the Arraylist, If it’s there then it returns true else it returns false.

  1. obj.contains("java");
obj.contains("java");

This returns true if the string “java” is present in the list else returns false.

10) clear() :

This is used to remove all the elements of the array list

  1. obj.clear();
obj.clear();

This removes all the elements of ArrayList.

When should we use ArrayList ?

when we want to have index based access

When we have requirement to add elements only at the end of list

When we don’t care about duplicate elements

When we should not use ArrayList ?

When we need to add elements at the beginning or any specific position of list

When we need to avoid duplicates as it allows duplicate elements

When we don’t need retrieval based on index.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on