Java ArrayList Class and Its Methods

By Preeti Jain Last updated : December 31, 2023

Java ArrayList Class

  • ArrayList is a class which is implemented by List interface in Collection framework.
  • ArrayList implement List interface and List interface is child interface of Collection interface so ultimately ArrayList can contains methods of List and Collection interface.

Java ArrayList Class: Collection Interface Methods

  1. boolean add(Object o)
  2. boolean addAll(Collection c)
  3. boolean remove(Object o)
  4. boolean removeAll(Collection c)
  5. boolean retainAll(Collection c)
  6. void clear()
  7. boolean isEmpty()
  8. int size()
  9. boolean contains(Object o)
  10. boolean containsAll(Collection c)
  11. Object[] toArray()
  12. Iterator iterator()

Example of Collection Interface Methods

We will understand the working of collection interface methods from the example given below:

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList al = new ArrayList();

    System.out.println("boolean add(Object o) : Add an object in ArrayList");
    al.add(10);
    al.add(20);
    al.add(30);
    al.add(40);
    al.add(50);
    System.out.println("Updated ArrayList is : " + al);

    System.out.println("boolean addAll(Collection c) : Add Collection in ArrayList");
    al.addAll(al);
    System.out.println("Updated ArrayList is : " + al);

    System.out.println("boolean contains(Object o) : it returns true if element contain");
    al.contains(10);
    System.out.println("Updated ArrayList is : " + al);

    System.out.println("boolean containsAll(Collection c) : it returns true if collection contain");
    al.containsAll(al);
    System.out.println("Updated ArrayList is : " + al);

    System.out.println("boolean toArray() : collection object convert into array");
    Object[] values = al.toArray();
    for (int i = 0; i < values.length; ++i)
      System.out.println("Display in Array form  : " + values[i]);

    System.out.println("boolean iterator() : it iterates collection object");
    Iterator it = al.iterator();

    while (it.hasNext())
      System.out.println("Iterating ArrayList : " + it.next());
  }
}

Output

D:\Java Articles>java CollectionMethods
boolean add(Object o) : Add an object in ArrayList
Updated ArrayList is : [10, 20, 30, 40, 50]
boolean addAll(Collection c) : Add Collection in ArrayList
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean contains(Object o) : it returns true if element contain
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean containsAll(Collection c) : it returns true if collection contain
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean toArray() : collection object convert into array
Display in Array form  : 10
Display in Array form  : 20
Display in Array form  : 30
Display in Array form  : 40
Display in Array form  : 50
Display in Array form  : 10
Display in Array form  : 20
Display in Array form  : 30
Display in Array form  : 40
Display in Array form  : 50
boolean iterator() : it iterates collection object
Iterating ArrayList : 10
Iterating ArrayList : 20
Iterating ArrayList : 30
Iterating ArrayList : 40
Iterating ArrayList : 50
Iterating ArrayList : 10
Iterating ArrayList : 20
Iterating ArrayList : 30
Iterating ArrayList : 40
Iterating ArrayList : 50

Java ArrayList Class: List Interface Methods

  1. boolean add(int index,Object o)
  2. boolean addAll(int index,Collection c)
  3. Object remove(int index)
  4. Object get(int index)
  5. Object set(int index,Object new)
  6. int indexOf(Object o)
  7. int lastIndexOf(Object o)
  8. ListIterator listiterator()

Example of List Interface Methods

We will understand the working of List interface methods from the example given below:

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList al = new ArrayList();
    LinkedList ll = new LinkedList();

    ll.add(10);
    ll.add(20);
    ll.add(30);
    ll.add(40);
    ll.add(50);

    al.add(10);
    al.add(20);
    al.add(30);
    al.add(40);
    al.add(50);
    System.out.println("Basic arraylist are :" + al);

    System.out.println(" add(int index,Object o): It adds an object to a specified index");
    al.add(2, 25);
    System.out.println("Updated arraylist are :" + al);

    System.out.println(" addAll(int index,Collection c): It adds collection to a specified index");
    al.addAll(6, al);
    System.out.println("Updated arraylist are :" + al);

    System.out.println(" get(int index): It returns object from a particular index");
    int value = (int) al.get(2);
    System.out.println("Return value of  :" + value);

    System.out.println(" indexOf(Object o): It returns an index of a particular object");
    int index = (int) al.indexOf(25);
    System.out.println("Return index of a particular object :" + index);

    System.out.println(" lastIndexOf(Object o): It returns a last occurrence index of a particular object");
    int last_index = (int) al.lastIndexOf(50);
    System.out.println("Return index of a particular object :" + last_index);

    System.out.println(" listIterator(): It iterates list object");
    ListIterator li = ll.listIterator();
    while (li.hasNext())
      System.out.println("Display objects in list form :" + li.next());
  }
}

Output

D:\Java Articles>java ListMethods
Basic arraylist are :[10, 20, 30, 40, 50]
add(int index,Object o): It adds an object to a specified index
Updated arraylist are :[10, 20, 25, 30, 40, 50]
addAll(int index,Collection c): It adds collection to a specified index
Updated arraylist are :[10, 20, 25, 30, 40, 50, 10, 20, 25, 30, 40, 50]
get(int index): It returns object from a particular index
Return value of  :25
indexOf(Object o): It returns an index of a particular object
Return index of a particular object :2
lastIndexOf(Object o): It returns a last occurrence index of a particular object
Return index of a particular object :11
listIterator(): It iterates list object
Display objects in list form :10
Display objects in list form :20
Display objects in list form :30
Display objects in list form :40
Display objects in list form :50

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.