Skip to main content

ArrayList Collection

Arraylist


  • Arraylist is a implementation class of list interface.
  •  Arraylist implement marker interface

    Serialize
    Clonable
    Random access

    
    public class ArrayList
    extends AbstractList
    implements List, RandomAccess, Cloneable, Serializable
    
    
  • Arraylist built on re-sizable array data structure.
  • Arralist size grows with a below formula.
new capacity=(old capacity 3/2)+1

Overloaded Constructor of ArrayList


    ArrayList()

    ArrayList(Collection c)

    ArrayList(int initialCapacity)

  • In Arraylist accessing and retrieval of element is faster compared to Linkedlist.
  • When to use ArrayList ?


    •  Arraylist preserve insertion order.
    • In arraylist each element has index so searching is easy if index is known.
    •  Duplicate object insertion is allowed
    • Null insertion is allowed.

    so if you want above functionality then array list is preferable.

    Example of Arraylist


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    package collectionConcept;
     
    import java.util.*;
     
     class Arraylist2
     { 
     
       public static void main(String args[])
       {  
     
         ArrayList al=new ArrayList();//creating arraylist  
         al.add("Ravi");//adding object in arraylist  
         al.add("Vijay");  
         al.add("Ravi");  
         al.add("Ajay");  
     
       Iterator itr=al.iterator();
       //getting Iterator from arraylist to traverse elements  
     
       while(itr.hasNext())
       {  
         System.out.println(itr.next());  
       }  
     
      }  
    }

    output:

    1
    2
    3
    4
    
    Ravi
    Vijay
    Ravi
    Ajay

    Methods of ArrayList


    1. boolean add(E e)

    Appends the specified element to the end of this list.

    2. void add(int index, E element)
    Inserts the specified element at the specified position in this list.

    3. boolean addAll(Collection c)
    Appends 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.

    4. boolean addAll(int index, Collection c)
    Inserts all of the elements in the specified collection into this list,
    starting at the specified position.

    5. void clear()
    Removes all of the elements from this list.

    6. Object clone()
    Returns a shallow copy of this ArrayList instance.

    7. boolean contains(Object o)
    Returns true if this list contains the specified element.

    8. void ensureCapacity(int minCapacity)
    Increases the capacity of this ArrayList instance, if necessary,
    to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

    9. E get(int index)
    Returns the element at the specified position in this list.

    10. int indexOf(Object o)
    Returns the index of the first occurrence of the specified element in this list,
    or -1 if this list does not contain the element.

    11. boolean isEmpty()
    Returns true if this list contains no elements.

    12. Iterator iterator()
    Returns an iterator over the elements in this list in proper sequence.

    13. int lastIndexOf(Object o)
    Returns the index of the last occurrence of the specified element in this list,
    or -1 if this list does not contain the element.

    14. ListIterator listIterator()
    Returns a list iterator over the elements in this list (in proper sequence).

    15. ListIterator listIterator(int index)
    Returns a list iterator over the elements in this list (in proper sequence),
    starting at the specified position in the list.

    16. E remove(int index)
    Removes the element at the specified position in this list.

    17. boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.

    18. boolean removeAll(Collection c)
    Removes from this list all of its elements that are contained in the specified collection.

    19. protected void removeRange(int fromIndex, int toIndex)
    Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

    20. boolean retainAll(Collection c)
    Retains only the elements in this list that are contained in the specified collection.

    21. E set(int index, E element)
    Replaces the element at the specified position in this list with the specified element.

    22. int size()
    Returns the number of elements in this list.

    23. List subList(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified fromIndex,
    inclusive, and toIndex, exclusive.

    24. Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).

    25. T[] toArray(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element);
    the runtime type of the returned array is that of the specified array.

    26. void trimToSize()
    Trims the capacity of this ArrayList instance to be the list’s current size.


    ArrayList Methods with Example


    1. boolean add(E e)
    Appends the specified element to the end of this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class AddElemnt 
    {
     
    	public static void main(String[] args) {
     
     
    		ArrayList<String> list=new ArrayList<String>();
     
     
    		//Adding element into Arraylist
     
    			list.add("Bhavesh");
     
    			list.add("Abhi");
     
    			list.add("vivek");
     
    			list.add("milan");
     
     
    			//Retrieving Array list Element
    			for (String s : list)
    			{
     
    				System.out.println(s);
    			}
     
    	}
    }

    Output:

    1
    2
    3
    4
    
    Bhavesh
    Abhi
    vivek
    milan

    2. void add(int index, E element)
    Inserts the specified element at the specified position in this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    
     
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class AddElemntThroughIndex 
    {
     
    	public static void main(String[] args) 
    	{
     
     
    	ArrayList<String> list=new ArrayList<String>();
     
    	//Adding element into Arraylist
     
    		list.add("Bhavesh");
     
    		list.add("Abhi");
     
    		list.add("vivek");
     
    		list.add("milan");
     
    		System.out.println("Size of Arraylist: "+list.size());
     
    		for (String s : list) 
    		{
     
    			System.out.println(s);
    		}
     
     
    		//Adding elemnt at Index 1
     
    		list.add(1,"Vaibhav");
     
    		System.out.println("After adding elemnt at index 1");
     
    		System.out.println("Size of Arraylist: "+list.size());
     
     
    		for (String s : list) 
    		{
     
    			System.out.println(s);
    		}
     
    	 }
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    Size of Arraylist: 4
    Bhavesh
    Abhi
    vivek
    milan
    After adding elemnt at index 1
    Size of Arraylist: 5
    Bhavesh
    Vaibhav
    Abhi
    vivek
    milan

    3. boolean addAll(Collection c)
    Appends 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
     
    public class AddAllMethod 
    {
     
    	public static void main(String[] args) 
    	{
     
    	    ArrayList<String> list1=new ArrayList<String>();
     
            list1.add("Bhavesh");
            list1.add("Abhilash");
     
     
            //retrieving list1 element
     
            System.out.println("Retrieving list 1 element");
            for (String s : list1) 
            {
     
            	System.out.println(s);
     
    		}
     
     
            ArrayList<String> list2=new ArrayList<String>();
     
            list2.add("Vivek");
            list2.add("milan");
     
            //Adding All element of list 1 into list 2
     
            list2.addAll(list1);
     
     
            System.out.println("Retrieving list 2:");
     
            for (String s : list2) 
            {
     
            	System.out.println(s);
    		}
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    
    Retrieving list 1 element
    Bhavesh
    Abhilash
    Retrieving list 2:
    Vivek
    milan
    Bhavesh
    Abhilash

    4. boolean addAll(int index, Collection c)
    Inserts all of the elements in the specified collection into this list,
    starting at the specified position.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class AddAllThroughindex 
    {
    	public static void main(String[] args) 
     
    	{
     
    	  ArrayList<String> list1=new ArrayList<String>();
     
          list1.add("Bhavesh");
          list1.add("Abhilash");
     
     
          //retrieving list1 element
     
          System.out.println("Retrieving list 1 element");
          for (String s : list1) 
          {
     
          	System.out.println(s);
     
          }
     
     
          ArrayList<String> list2=new ArrayList<String>();
     
          list2.add("Vivek");
          list2.add("milan");
     
          //Adding collection into list 2 at index 0
     
          list2.addAll(0,list1);
     
          System.out.println("Retrieving list 2:");
     
          for (String s : list2) 
          {
     
          	System.out.println(s);
     
    	  }
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    
    Retrieving list 1 element
    Bhavesh
    Abhilash
    Retrieving list 2:
    Bhavesh
    Abhilash
    Vivek
    milan

    5. void clear()
    Removes all of the elements from this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    //This Example shows the use of isEmpty and Clear method of Arraylist
     
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class Clear 
    {
     
    	public static void main(String[] args) {
     
    		System.out.println("Starting Program");
     
    		ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
     
     
    		//Clearing arraylist
    		  emp_list.clear();
     
    		System.out.println("After clearing Arraylist");
     
    		//Retrieving Arraylist
    		  for (String s : emp_list) 
    			{
     
    				System.out.println(s);
    			}
     
     
    		  //method returns true if this list contains no elements
     
    		   boolean b=emp_list.isEmpty();
    		   System.out.println("Arraylist Empty :"+b);
     
     
     
    		 System.out.println("ending program");
    	  }
    }

    Output:

    1
    2
    3
    4
    
    Starting Program
    After clearing Arraylist
    Arraylist Empty :true
    ending program

    6. Object clone()
    Returns a shallow copy of this ArrayList instance.
    package com.spy.arraylist.method;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    import java.util.ArrayList;
     
    public class Clone 
    {
     
    	public static void main(String[] args)
    	{
     
            ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
    		//Retrieving Arraylist element
     
    		for (String s : emp_list) 
    		{
     
    			System.out.println(s);
    		}
     
    		//Making shallow copy
     
    		ArrayList<String> copy_emp=(ArrayList)emp_list.clone();	
     
    		System.out.println("Retrieving element from shallo copy of Arraylist");
     
    		//Retrieving Arraylist element of shallow copy
    		for (String s : copy_emp) 
    		{
     
    			System.out.println(s);
    		}
     
    	}
     
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    Retrieving element from shallo copy of Arraylist
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati

    7. boolean contains(Object o)
    Returns true if this list contains the specified element.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class ContainMethod 
    {
     
    	public static void main(String[] args) 
    	{
     
            ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
     
     
    		for (String s : emp_list) 
    		{
     
    			System.out.println(s);
    		}
     
     
    		System.out.println("**************************");
     
    		//This method returns true if element present in Arraylist
    		boolean b=emp_list.contains("Bhavesh Lakhani");
     
    	    if(b)
    	    {
    	    	System.out.println("Bhavesh Lakhani present in ArrayList");
    	    }
     
    	    else
    	    {
    	    	System.out.println("Element not present in Arraylist");
    	    }
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    **************************
     
    Bhavesh Lakhani present in ArrayList

    8. void ensureCapacity(int minCapacity)
    Increases the capacity of this ArrayList instance, if necessary,
    to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class IncreaseSize 
    {
     
    	public static void main(String[] args)
    	{
     
            ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
    		//Increasing size of ArrayList
    		emp_list.ensureCapacity(15);
     
    	    //Retrieving Element
    		for (String s : emp_list) 
    		{
    			System.out.println(s);
    		}
     
    	}
    }

    Output:

    1
    2
    3
    4
    
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati

    9. E get(int index)
    Returns the element at the specified position in this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class GetElemnt 
    {
    	public static void main(String[] args) 
     
    	{
     
    	 ArrayList<String> list=new ArrayList<String>();
     
     
    		list.add("Bhavesh");
     
    		list.add("Abhi");
     
    		list.add("vivek");
     
    		list.add("milan");
     
     
     
    		//getting element at an index 0
    		System.out.println("Getting element at index 0: "+list.get(0));
     
     
    		System.out.println("Retrieving Arraylist element using for loop ");
    		System.out.println("****************************************");
     
    	   // Retrieving Arraylist element using for loop
     
    		for(int i=0;i<list.size();i++)
    		{
     
    			String name=list.get(i);
     
    			System.out.println(name);
     
    		}	
     
    	}
    }
     
    <strong>Output:</strong>
    <pre lang="java" line="1">
    Getting element at index 0: Bhavesh
    Retrieving Arraylist element using for loop 
    ****************************************
    Bhavesh
    Abhi
    vivek
    milan

    10. int indexOf(Object o)
    Returns the index of the first occurrence of the specified element in this list,
    or -1 if this list does not contain the element.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class indexofMethod 
    {
    	public static void main(String[] args) 
    	{
     
    		ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
     
    	   int index=emp_list.indexOf("Bhavesh Lakhani");
     
    	   System.out.println("Index number of Bhavesh Lakhani: "+index);
     
     
     
    	   int i=emp_list.indexOf("Vaibhav Prajapati");
     
    	   System.out.println("Index number of Vaibhav Prajapati: "+i);
     
    	}
    }

    Output:

    1
    2
    
    Index number of Bhavesh Lakhani: 0
    Index number of Vaibhav Prajapati: 3

    11. boolean isEmpty()
    Returns true if this list contains no elements.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    //This Example shows the use of isEmpty and Clear method of Arraylist
     
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class Clear 
    {
     
    	public static void main(String[] args) {
     
    		System.out.println("Starting Program");
     
    		ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
     
     
    		//Clearing arraylist
    		  emp_list.clear();
     
    		System.out.println("After clearing Arraylist");
     
    		//Retrieving Arraylist
    		  for (String s : emp_list) 
    			{
     
    				System.out.println(s);
    			}
     
     
    		  //method returns true if this list contains no elements
     
    		   boolean b=emp_list.isEmpty();
    		   System.out.println("Arraylist Empty :"+b);
     
     
     
    		 System.out.println("ending program");
    	  }
    }

    Output:

    1
    2
    3
    4
    
    Starting Program
    After clearing Arraylist
    Arraylist Empty :true
    ending program

    12. Iterator iterator()
    Returns an iterator over the elements in this list in proper sequence.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    package com.spy.javaforlearn;
     
    import java.util.ArrayList;
    import java.util.Iterator;
     
    public class IteratorExample 
    {
     
    	public static void main(String[] args) 
    	{
    		 ArrayList<String> list=new ArrayList<String>();
     
     
    			list.add("Bhavesh");
     
    			list.add("Abhi");
     
    			list.add("vivek"); 
     
    			list.add("milan");
     
    			//Retrieving Arraylist element using Iterator
     
    				System.out.println("Retrieving element using for Iterator");
    				System.out.println("**************************************");
     
    				Iterator<String> it=list.iterator();
     
    				while(it.hasNext())
    				{
     
    					System.out.println(it.next());
    				}	
     
    	     }
    }

    Output:

    1
    2
    3
    4
    5
    6
    
    Retrieving element using for Iterator
    **************************************
    Bhavesh
    Abhi
    vivek
    milan

    13. int lastIndexOf(Object o)
    Returns the index of the last occurrence of the specified element in this list,
    or -1 if this list does not contain the element.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class LastIndex 
    {
     
    	public static void main(String[] args) 
    	{
     
    		  ArrayList<String> emp_list=new ArrayList<>();
     
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Abhilash Shah");
    			emp_list.add("Nilay Shah");
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Vaibhav Prajapati");
     
    	 	   System.out.println("Retrieving element........");	
     
    		 for (String s : emp_list) 
    		 {
     
    			 System.out.println(s);
     
    		 }	
     
    		//getting the last index of Object or element 
    		int index= emp_list.lastIndexOf("Bhavesh Lakhani");
     
    		System.out.println("last index of Bhavesh Lakhani: "+index);
     
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    
    Retrieving element........
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Bhavesh Lakhani
    Vaibhav Prajapati
    last index of Bhavesh Lakhani: 3

    14. ListIterator listIterator()
    Returns a list iterator over the elements in this list (in proper sequence).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    package com.spy.javaforlearn;
     
    import java.util.ArrayList;
    import java.util.Iterator;
     
    public class ListIterator 
    {
    	public static void main(String[] args) 
    	{
     
    		ArrayList<String> list=new ArrayList<>();
     
    		list.add("Milan");
    		list.add("nilay");
    		list.add("Vaibhav");
    		list.add("alex");
     
    	    //Retrieving element using list Iterator
     
    		java.util.ListIterator<String> it=list.listIterator();
     
     
     
     
    		//accessing element from first index
     
    				System.out.println("accessing element from first index.......");
     
    				while(it.hasNext())
    				{
     
    					System.out.println(it.next());
     
    				}
     
     
     
    		//accessing element from last index
     
    		System.out.println("accessing element from last index.......");
     
    		while(it.hasPrevious())
    		{
    	     	System.out.println(it.previous());
    	    }
     
    	}
     
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    accessing element from first index.......
    Milan
    nilay
    Vaibhav
    alex
    accessing element from last index.......
    alex
    Vaibhav
    nilay
    Milan

    15. ListIterator listIterator(int index)
    Returns a list iterator over the elements in this list (in proper sequence),
    starting at the specified position in the list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    package com.spy.javaforlearn;
     
    import java.util.ArrayList;
    import java.util.Iterator;
     
    public class ListIterator2 
    {
    	public static void main(String[] args) 
    	{
     
    		ArrayList<String> list=new ArrayList<>();
     
    		list.add("Milan");
    		list.add("nilay");
    		list.add("Vaibhav");
    		list.add("alex");
     
    	    //Retrieving element using list Iterator and statrting from index 1
     
    		Iterator<String> it=list.listIterator(1);
     
     
    		//accessing element from first index
     
    				System.out.println("accessing element from first index.......");
     
    				while(it.hasNext())
    				{
     
    					System.out.println(it.next());
     
    				}
     
    	    }
    }

    Output:

    1
    2
    3
    4
    
    accessing element from first index.......
    nilay
    Vaibhav
    alex

    16. E remove(int index)
    Removes the element at the specified position in this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class RemoveElement 
    {
     
    	public static void main(String[] args) 
    	{
     
    		// Adding and Removing element from Arraylist
     
    				ArrayList<String> list=new ArrayList<String>();
     
     
    				//Adding element into Arraylist
     
    				list.add("Bhavesh");
     
    				list.add("Abhi");
     
    				list.add("vivek");
     
    				list.add("milan");
     
    				System.out.println("size of arraylist "+list.size());
     
    			// Retrieving Arraylist element using for loop
     
    				for(int i=0;i<list.size();i++)
    				{
     
    					String name=list.get(i);
     
    					System.out.println(name);
     
    				}
     
    				//removing Arraylist element from index 2
     
    				list.remove(2);
     
    				System.out.println("After removing element from index 2");
     
    				System.out.println("size of arraylist "+list.size());
     
    				for(int i=0;i<list.size();i++)
    				{
     
    					String name=list.get(i);
     
    					System.out.println(name);
     
    				}
    		}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    size of arraylist 4
    Bhavesh
    Abhi
    vivek
    milan
    After removing element from index 2
    size of arraylist 3
    Bhavesh
    Abhi
    milan

    17. boolean remove(Object o)
    Removes the first occurrence of the specified element from this list, if it is present.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class Remove2 
    {
     
    	public static void main(String[] args) 
    	{
     
     
            ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Vaibhav Prajapati");
     
     
    		System.out.println("Retrieving Element");
     
    		for (String s : emp_list) 
    		{
    		  System.out.println(s);
    		}
     
    		// This method removes the first occurrence of the specified element from this list, if it is present.
    		   emp_list.remove("Bhavesh Lakhani");
     
    		System.out.println("After Removing first occurence of Bhavesh Lakhani");
     
    		for (String s : emp_list) 
    		{
    			System.out.println(s);
    		}	
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    Retrieving Element
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Bhavesh Lakhani
    Vaibhav Prajapati
    After Removing first occurence of Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Bhavesh Lakhani
    Vaibhav Prajapati

    18. boolean removeAll(Collection c)
    Removes from this list all of its elements that are contained in the specified collection.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class RemoveAll 
    {
     
    	public static void main(String[] args) 
    	{
     
    		 ArrayList<String> emp_list=new ArrayList<>();
     
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Abhilash Shah");
    			emp_list.add("Nilay Shah");
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Vaibhav Prajapati");
     
     
    			System.out.println("List 1 Element");
    			System.out.println("***************");
     
    			for (String s : emp_list) 
    			{
    			  System.out.println(s);
    			}
     
     
     
    			ArrayList<String> emp_list2=new ArrayList<>();
     
    			emp_list2.add("Bhavesh Lakhani");
    			emp_list2.add("Nilay Shah");
     
    			System.out.println("List 2 element");
    			System.out.println("***************");
     
    			for (String s : emp_list2)
    			{
    			System.out.println(s);	
    			}
     
     
    			 //Removing element from emp_list1 which is present in emp_list2
    			 emp_list.removeAll(emp_list2);
     
    			System.out.println("After Removing element from emp_list1 which is present in emp_list2 ");
     
    			for (String s : emp_list) 
    			{
    				System.out.println(s);
    			}	
     
        	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    List 1 Element
    ***************
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Bhavesh Lakhani
    Vaibhav Prajapati
    List 2 element
    ***************
    Bhavesh Lakhani
    Nilay Shah
     
    After Removing element from emp_list1 which is present in emp_list2 
    Abhilash Shah
    Vaibhav Prajapati

    19. protected void removeRange(int fromIndex, int toIndex)
    Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    
     
    <strong>20. boolean	retainAll(Collection<?> c)</strong>
    Retains only the elements in this list that are contained in the specified collection.
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class RetainAll 
    {
     
    	public static void main(String[] args) 
     
    	{
     
    		 ArrayList<String> emp_list=new ArrayList<>();
     
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Abhilash Shah");
    			emp_list.add("Nilay Shah");
    			emp_list.add("Vaibhav Prajapati");
     
     
    			System.out.println("List 1 Element");
    			System.out.println("***************");
     
    			for (String s : emp_list) 
    			{
    			  System.out.println(s);
    			}
     
    			ArrayList<String> emp_list2=new ArrayList<>();
     
    			emp_list2.add("Bhavesh Lakhani");
    			emp_list2.add("Nilay Shah");
     
    			System.out.println("List 2 element");
    			System.out.println("***************");
     
    			for (String s : emp_list2)
    			{
    			System.out.println(s);	
    			}
     
     
    			 //Removing element from emp_list1 which is present in emp_list2
    			 emp_list.retainAll(emp_list2);
     
    			System.out.println("Now in emp_list1 contain only emp_list2");
     
    			for (String s : emp_list) 
    			{
    				System.out.println(s);
    			}	
     
     
     
    	}
     
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    List 1 Element
    ***************
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    List 2 element
    ***************
    Bhavesh Lakhani
    Nilay Shah
    Now in emp_list1 contain only emp_list2
    Bhavesh Lakhani
    Nilay Shah

    21. E set(int index, E element)
    Replaces the element at the specified position in this list with the specified element.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class SetMethod 
    {
     
    	public static void main(String[] args) 
    	{
     
    		ArrayList<String> list=new ArrayList<>();
     
    		list.add("Vivek");
    		list.add("milan");
    		list.add("Abhishek");
     
    		for (String s : list) 
    		{
    			System.out.println(s);
    		}
     
            // Replacing or Updating element at index 0	
     
    		list.set(0,"Bhavesh");
     
     
    		System.out.println("After Replacing element at index 0");
     
    		for (String s : list) 
    		{
    			System.out.println(s);
    		}
     
    	}
     
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    
    Vivek
    milan
    Abhishek
    After Replacing element at index 0
    Bhavesh
    milan
    Abhishek

    22. int size()
    Returns the number of elements in this list.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class ArrayList_size 
    {
    	public static void main(String[] args) {
     
     
    	ArrayList<String> list=new ArrayList<String>();
     
     
    	//Adding element into Arraylist
     
    		list.add("Bhavesh");
     
    		list.add("Abhi");
     
    		list.add("vivek");
     
    		list.add("milan");
     
    		System.out.println("Size of Arraylist: "+ list.size());
     
    		//Retrieving Array list Element
    		for (String s : list)
    		{
     
    			System.out.println(s); 
    		}
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    6
    
     
    Size of Arraylist: 4
    Bhavesh
    Abhi
    vivek
    milan

    23. List subList(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified fromIndex,
    inclusive, and toIndex, exclusive.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
    import java.util.List;
     
    public class Sublist
    {
     
    	public static void main(String[] args) {
     
     
    		ArrayList<String> list=new ArrayList<String>();
     
     
    		//Adding element into Arraylist
     
    			list.add("Bhavesh");
     
    			list.add("Abhi");
     
    			list.add("vivek");
     
    			list.add("milan");
     
     
    		  List<String>  sublist= list.subList(1, 3);
     
    		  System.out.println("Retrieving sub list");
    		  for (String s : sublist) 
    		  {
     
    			  System.out.println(s);
     
    		   }
     
     
     
    		}
    }

    Output:

    1
    2
    3
    4
    
     
    Retrieving sub list
    Abhi
    vivek

    24. Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence (from first to last element).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class CovertingToArray 
    {
     
    	public static void main(String[] args) 
    	{
     
            ArrayList<String> emp_list=new ArrayList<>();
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
    		System.out.println("Retrieving element of Arraylist......");
     
    		for (String s : emp_list) 
    		{
    			 System.out.println(s);
     
    		}
     
    		// Converting Arraylist collection  to Array
    		Object[] emp_name=(Object[]) emp_list.toArray();
     
    		System.out.println("Retrieving element of Array......");
     
    		for(int i=0;i<emp_name.length;i++)
    		{
     
    			System.out.println(emp_name[i]);
    		}
     
     
    	}
     
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
     
    Retrieving element of Arraylist......
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    Retrieving element of Array......
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati

    25. T[] toArray(T[] a)
    Returns an array containing all of the elements in this list in proper sequence (from first to last element);
    the runtime type of the returned array is that of the specified array.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class Toarray 
    {
     
    	public static void main(String[] args) 
    	{
     
     
    		 ArrayList<String> emp_list=new ArrayList<>();
     
    			emp_list.add("Bhavesh Lakhani");
    			emp_list.add("Abhilash Shah");
    			emp_list.add("Nilay Shah");
    			emp_list.add("Vaibhav Prajapati");
     
    			System.out.println("Retrieving element of Arraylist......");
     
    			for (String s : emp_list) 
    			{
    				 System.out.println(s);
     
    			}
     
    			String[] s=new String[emp_list.size()];
     
    			//toarray copies element into another Array
    	         s=emp_list.toArray(s);
     
    			System.out.println("Retrieving element of Array......");
     
    			for(int i=0;i<s.length;i++)
    			{
     
    				System.out.println(s[i]);
    			}
    	  }
    }

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
     
    Retrieving element of Arraylist......
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    Retrieving element of Array......
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati

    26. void trimToSize()
    Trims the capacity of this ArrayList instance to be the list’s current size.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
    // Trims the capacity of this ArrayList instance to be the list's current size.
    //An application can use this operation to minimize the storage of an ArrayList instance.
     
    package com.spy.arraylist.method;
     
    import java.util.ArrayList;
     
    public class TrimSize 
    {
     
     
    	public static void main(String[] args) 
    	{
     
     
    		ArrayList<String> emp_list=new ArrayList<>(20);
     
    		emp_list.add("Bhavesh Lakhani");
    		emp_list.add("Abhilash Shah");
    		emp_list.add("Nilay Shah");
    		emp_list.add("Vaibhav Prajapati");
     
    		//Trims the capacity of this ArrayList instance to be the list's current size.
     
    		 emp_list.trimToSize();
     
    		for (String s : emp_list) 
    		{
     
    			System.out.println(s);
    		}
     
    	}
    }

    Output:

    1
    2
    3
    4
    5
    
     
    Bhavesh Lakhani
    Abhilash Shah
    Nilay Shah
    Vaibhav Prajapati
    Different ways to retrieve Arraylist element
    1. Using for loop
    2. Using for each loop
    3. Using Iterator
    4. Using Listiterator