Home » Java programming language

Explain java ArrayList and its methods



Learn: Explain Java ArrayList Methods? Here we are going to learn in brief ArrayList methods in java?
Submitted by Preeti Jain, on February 10, 2018

ArrayList in java

  • 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.

1) 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:

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

import java.util.*;

class CollectionMethods{
	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

2) 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:

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

import java.util.*;

class ListMethods{
	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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.