Java ArrayList

ArrayList is dynamic array which contains duplicate elements. It extends AbstractList class and implements List interface.

Capacity Of ArrayList

Default capacity of ArrayList is 10. You can also specify your own while creating arraylist.

Advantage over Array

  1. Dynamic as it can grow at run-time unlike as array.
  2. Initial Capacity can be provided while creating ArrayList object.
  3. ArrayList class can contain duplicate elements.
  4. ArrayList class maintains insertion order meaning retrieval will be as inserted.
  5. ArrayList allows random access because it internally use array for keeping elements.

 Disadvantage

  1. Each element new element added it checks if exceeds the capacity then a new array is created with multiple of 3/2 and all elements are being copied to new one.Lots of shifting operation while adding element in middle.
  2. No any data structure algorithm has been used for this collection.
  3. ArrayList is not synchronized. Means in multithreading, you can’t share the instance of arraylist between threads.

Constructors

  • ArrayList()   :- Default constructor
  • ArrayList(Collection c) – Here we can put any other collection or another ArrayList while creating object.
  • ArrayList(int capacity) – Here we can define the initial capacity of ArrayList.

Methods

  • void add(int index, Object element)  :- To add element to specified position.
  • boolean add(Object o) :- To add element at end of list.
  • boolean addAll(Collection c) :- To add all elements.
  • void clear() :- To clear all elements of list.
  • Object[] toArray() :- To convert ArrayList elements to Object[].
  • int size() : –  It returns the number of elements within the list.
  • boolean isEmpty() :- It returns the boolean true if list is empty otherwise false.
  • Object get(int index) throws IndexOutOfBoundsException :- It returns the element stored at given index and throw IndexOutOfBoundsException if element not found in given index.
  • Object set(int index,Object element) :- It used to replace the previous element stored at given index and throw IndexOutOfBoundsException if index doesn’t found.
  • boolean contains(Object element) :- It checks whether the element is present in list or not.

Read More

Java Non-generic Vs Generic Collection

Before JDK 1.5, Java collection framework was non-generic. Since 1.5, it is generic.  In non-generic collection there was no type safety. We can add any types of object as element to the collection and at run time we need to typecast them accordingly to use. This problem has been resolved in generic collection framework as we can specify the type of element to be added.

Non-generic example of creating java collection object

Old way to create non-generic arraylist. Here we can add any type of object and need to typecast each time before use.

ArrayList list=new ArrayList();

Generic example of creating java collection

Here we can add only String type of objects and no typecasting required.

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

Java ArrayList Example

Here adding element with or without index to the list.


package com.startwithjava;

import java.util.*;

public class ArrayListTest{

public static void main(String arr[]){

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

list.add("List 1");
list.add(0,"List 2"); //it will replace first element List 1
list.add("List 3");
list.add("List 4");

//To traverse arraylist elements,we need Iterator. iterator() method is used get the Iterator  object.

Iterator itr = list.iterator();

//hasNext() method is used to check next element of a collection. It returns true if next elements exits otherwise false returned.

while(itr.hasNext()){

//next() method return the object/element.

System.out.println("Value:-"+itr.next());

}

}

}

Output

 List 2
 List 3
 List 4

Two ways to iterate the elements of ArrayList in java

There are two ways to traverse collection elements:

  1. By Iterator interface.
  2. By for-each loop.

Iterate ArrayList using Iterator Interface

import java.util.*; 
class TestCollection2{ 
 public static void main(String args[]){ 
 ArrayList<String> list=new ArrayList<String>();
 list.add("Ravi"); 
 list.add("Mohan"); 
 list.add("Ravi"); 
 list.add("Sonu"); 
 Iterator itr = list.iterator();
 while(itr.hasNext()){
  System.out.println("Name:- "+itr.next());
 }
}

Iterate ArrayList using for-each loop

import java.util.*; 
class TestCollection2{ 
 public static void main(String args[]){ 
 ArrayList<String> list=new ArrayList<String>();
 list.add("Ravi"); 
 list.add("Mohan"); 
 list.add("Ravi"); 
 list.add("Sonu"); 
 for(String name:list) 
 System.out.println(name); 
 } 
}

Get the size of ArrayList Example

The size() method is used to know the number of elements within the ArrayList.


package com.startwithjava;

import java.util.*;

public class ArrayListTest{

public static void main(String arr[]){

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

list.add("List 1");
list.add("List 2");
list.add("List 3");
System.out.println("Size:- "+list.size());

}
}

Output

Size:- 3

Example of addAll(Collection c) method

Here adding all elements of one list to another list using addAll() method.


package com.startwithjava;

import java.util.*;

public class ArrayListTest{

public static void main(String arr[]){

ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();

list1.add("List 1");
list1.add("List 2");
list1.add("List 3");
list2.addAll(list1);

Iterator itr = list2.iterator();
while(itr.hasNext()){
System.out.println("Value:-"+itr.next());
}

}

}

Output

 List 1
 List 2
 List 3

Leave a Reply