Arraylist in Java

ArrayList class is based on the List Interface in java which is based on array data structure. ArrayList is widely used for its functionality and flexibility it offers.

ArrayLists are resizeable list of array implementations of list interface. It implements all optional list operations and permits all elements incuding null.

Interface

ArrayList

LinkedList

What is the difference between ArrayList and Array?

Arrays are fixed length data structure so if it get filled up totally then there is no space left to to add more elements to it. Likewise if the number of elements gets removed from the array then the memory consumption would still be the same as the array size will not shrink automatically.

Whereas, ArrayList can dynamically grow in size and shrink in size after addition and removal of the elements. Apart from these the ArrayList data structure allows us to use pre-defined methods that are built in the collections framework in Java these methods make our task easy.

For Example:

Adding an element in the array at a specified position:

10    11    12    13    14    15    16   17    18    19 the original array

0     1     2      3     4    5     6    7     8      9 index position of the elements

adding an element 20 at position 3

10    11    12    20    13    14    15    16    17   18   19 the new array

0     1     2     3     4     5      6    7     8     9   10 new index position of the elements

 

Syntax to create an ArrayList

ArrayList<type> variable name = new ArrayList<type>();

type define anything like Strings and Integers etc.

Adding elements to the ArrayList

To easily add elements to the arraylist we use the predefined method: add()

Add(element) :  add() method adds the given elements at the end of the array.

example: ArrayList<String> name = new ArrayList<String>();

name.add("Tom");

name.add("Jerry");

So the output will look something like this : [Tom, Jerry].

We can also add elements to a specified index using the add() method

add(index, element) : This is another variation of the method and will add the given element to the specified index.

eg:     add(1, "Oggy");

So the final list of output will look something like this : [Tom, Oggy, Jerry].

Change an element in the ArrayList:

We use the set() method to change the element in the list, we give the index position and the element to be inserted as parameters ot the set() method and it will insert the new element and remove the old element in the ArrayList

Syntax:

name.set(index, element)

example:

name.set(0, "Bruce");

So the final list of output will look something like this : [Bruce, Oggy, Jerry].

Remove an element from the ArrayList:

Remove method removes the array element from the desired index position

Syntax: remove(index)

Size of the ArrayList:

We can use the arraylist size() method to find the size of the number of elements present in the arraylist

Syntax: size()

Example: names.size()

Sort() on the ArrayList:

Sort() method is in the Collections class and is the part of java.util package. This method sorts the arraylist. This also works with the numeric list.

Syntax:  Collections.sort(ArrayList variable);

Example : ArrayList<String> name = new ArrayList<String>();

Collections.sort(name);

 

Some More Methods of ArrayList class

  1. add( element): This method adds an object o to the arraylist.
  2. add(int index, element): It adds the object o to the array list at the given index.
  3. remove(element): removes the given element 
  4. remove(int index): removes the element from the given index
  5. set(int index, element):updating an element. It replaces the element present at the specified index with the new element.
  6.  int indexOf(element) : Give the index of the element 
  7. Object get(int index) : retuens the objects index present in the list
  8.  int size(): gives the size of the ArrayList – Number of elements of the list.