Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday 8 April 2017

ArrayList in Java

 

Java ArrayList Class

ArrayList is a class in java collection framework. ArrayList class extends AbstractList class and implements List interface. ArrayList class uses dynamic array for storing the elements. ArrayList support dynamic arrays that can grow as needed.

Java ArrayList is the replacement of the Vector class in collection. It is a new class in java.


Some of the important points about ArrayList class shown below

  • Java ArrayList class can contains duplicate elements.
  • Java ArrayList class maintains insertion order of the elements.
  • Size of ArrayList can be dynamically increased or decreased.
  • Java ArrayList class is not synchronized.
  • Java ArrayList supports dynamic array which can grows as needed.
  • Java ArrayList class elements can be access randomly.
  • ArrayList class allows random access because array works as index basis.

Hierarchy of ArrayList class in Java

ArrayList in Java

In the above diagram, ArrayList class extends AbstractList class and AbstractList class implements List interface. The List interface extends collection interface and collection interface extends top most interface of collection framework Iterable interfacce.


Java ArrayList class declaration

Declaration of jav.util.ArrayList class.

public class ArrayList<E> extends AbstractList<E> implements List<E>


Java ArrayList Constructor

(1) ArrayList()

This constructor is used to create an empty array list.

(2) ArrayList(Collection c)

This constructor is used to create an array list that is initialized with the elements of the collection c.

(3) ArrayList(int capacity)

This constructor is used to create an array list that has the specified the initial capacity.


ArrayList Methods

1) void add(int index, Object element)

This method is used to insert the specified elements at the specified position index in a list.

2) boolean addAll(Collection c)

This method is used to add all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

3) void clear()

This method is used to clear or remove all the elements from the list.

4) Object[] toArray()

This method return an array containing all of the elements in this list in the correct order.

5) Object[] toArray(Object[] a)

This method return an array containing all of the elements in this list in the correct order.

6) boolean add(Object o)

This method is used to add the elements to the end of the list.

7) boolean addAll(int index, Collection c)

This method is used insert all of the elements in the specified collection into this list, starting at the specified position.

8) Object clone()

This method returns shallow copy of an array list class.

9) int indexOf(Object o) 

Return the index in this list of the first occurrence of the specified elements , or -1 if the list does not contain this element.

10) void trimToSize()

It is used to trim the capacity of array list instance to be the list's current size.


Generic or Non-Generic Collection in Java

Before jdk 1.5 collection was non-generic, since 1.5 java collection framework is generic.

Old non-generic example of collection framework

ArrayList al = new ArrayList();//creating non-generic array list

Now, new Generic collection allow you to have only one type of object in collection. It is type safe so typecasting is not required at runtime.

New generic example of collection framework

ArrayList<String> al = new ArrayList<String>();//generic array list

In new generic collection we specify the type in angular braces.


Java ArrayList Example

import java.util.*;
class ArrayListExample
{
public static void main(String args[])
{
ArrayList<String> al = new ArrayList<String>();//create array list
al.add("India");//add object in array list
al.add("America");
al.add("Brazil");
al.add("Nepal");
al.add("England");
al.add("India");

//traverse elements through Iterator

Iterator it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

output : India
              America
              Brazil
              Nepal
              England
              India

above arraylist example contains duplicate elements and maintain insertion order. In the above example we need to import java.util package to execute the collection program.


Iterate the elements by 2 ways in collection

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

In the above ArrayList example we used iterator interface to fetch the elements in collection. Now we are going to use for-each loop to traverse the elements in collection.

Traverse Elements through for-each loop

import java.util.*;
class ArrayListExample1
{
public static void main(String args[])
{
ArrayList<String> all = new ArrayList<String>();
all.add("Mango");
all.add("Mango");
all.add("banana");
all.add("Orange");

//Using for-each loop

for(String s : all)
{
System.out.println(s);
}
}
}

output : Mango
              Mango
              banana
              Orange


ArrayList addAll Method

In the below example we are going to use addAll(Collection c) method to adding one list with another list.

import java.util.*;
class AddAllExample
{
public static void main(String args[])
{
ArrayList<String> al1 = new ArrayList<String>();
al1.add("anurag");
al1.add("siya");
al1.add("simmu");

//creating another arraylist

ArrayList<String> al2 = new ArrayList<String>();
al2.add("siddhart");
al2.add("sumit");

//adding al2 list with al1 list

al1.addAll(al2);

Iterator itr = al1.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}


output:  anurag
              siya
              simmu
              siddhart
              sumit


ArrayList removeAll Method


import java.util.*;
class RemoveAllExample
{
public static void main(String args[])
{
ArrayList<String> al1 = new ArrayList<String>();
al1.add("anurag");
al1.add("siya");
al1.add("simmu");

//creating another arraylist

ArrayList<String> al2 = new ArrayList<String>();
al2.add("siddhart");
al2.add("sumit");

//adding al2 list with al1 list

System.out.println("before removing al2 list from al1 list");
System.out.println();

al1.addAll(al2);

Iterator itr = al1.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}

//remove al2 list from al1

al1.removeAll(al2);
System.out.println("After removing al2 list from al1 list");
System.out.println();
for(String s : al1)
{
System.out.println(s);
}
}


output:  before removing al2 list from al1 list
             
              anurag
              siya
              simmu
              siddhart
              sumit
             After removing al2 list from al1 list
            
            anurag
            siya
            simmu
              
            
                            


Share:

1 comment:

  1. In this post you have explain arraylist in good way.one more arraylist resource visit java vogue

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate