Home » Java » ArrayList Important methods

ArrayList Important Methods

Following are some of the most important ArrayList methods.

1 . Adding elements to the list

boolean add(Element e)

This method will add the specified element to the end of this list.

// Adding items to arrayList
list.add("Item1");
list.add("Item2");

void add(int index, Element e)

list.add(2, "Item3"); // it will add Item3 to the third position of
					  // array list
					  

This method will add the specified element at the specified position in the list.


2. Getting the size of the list

public int size()

By using size() method of ArrayList we can determine the size of the ArrayList. This method returns the number of elements of ArrayList.

Example :

package com.example;

import java.util.ArrayList;

public class ArrayListSize {
	public static void main(String[] args) {
		ArrayList<Integer> al = new ArrayList<Integer>();
		System.out.println("Initial size of Array is: " + al.size());
		al.add(1);
		al.add(2);
		al.add(35);
		al.add(54);
		al.add(99);
		System.out.println("Size after addition of elements: " + al.size());

	}
}

Output :

Initial size of Array is: 0
Size after addition of elements: 5


3. Adding elements to the list

boolean add(Element e)

This method is used for adding an element to the ArrayList.


Example :


package com.example;

import java.util.ArrayList;

public class ArrayListAdd {
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		// add method for String ArrayList
		al.add("Raj");
		al.add("Ravi");
		al.add("Mukesh");
		System.out.println("Elements of ArrayList is: " + al);

	}
}

Output :

Elements of ArrayList is: [Raj, Ravi, Mukesh]

void add(int index, Element e)

This method adds the specified element at the specified position in the list.

Example :


package com.example;

import java.util.ArrayList;

public class ArrayListAdd {
	public static void main(String[] args) {
		// ArrayList of String type
		ArrayList<String> al = new ArrayList<String>();
		al.add("java");
		al.add("is");
		al.add("easy");
		al.add("to");
		System.out.println("Elements before adding string learn:" + al);
		/*
		 * adding element to the 5th position = 4th index as index starts with 0
		 */
		al.add(4, "learn");

		System.out.println("Elements after adding string learn:" + al);

	}
}

Output :

Elements before adding string learn:[java, is, easy, to]
Elements after adding string learn:[java, is, easy, to, learn]

public boolean addAll(Collection c)

This method is used for adding all the elements of a list to the another list.

It adds all the elements of specified Collection c to the current list.

Example :


package com.example;

import java.util.ArrayList;

public class ArrayListAdd {
	public static void main(String[] args) {
		// ArrayList of String type
		ArrayList<String> al = new ArrayList<String>();
		al.add("java");
		al.add("is");
		al.add("easy");
		al.add("to");
		al.add("learn");
		System.out.println("ArrayList1 before addAll:" + al);

		// ArrayList2 of String Type
		ArrayList<String> al2 = new ArrayList<String>();
		al2.add("Use");
		al2.add("Eclipse");
		al2.add("IDE");
		al2.add("for");
		al2.add("programming");

		// Adding ArrayList2 into ArrayList1
		al.addAll(al2);
		System.out.println("ArrayList1 after addAll:" + al);
	}
}

Output :

ArrayList1 before addAll:[java, is, easy, to, learn]
ArrayList1 after addAll:[java, is, easy, to, learn, Use, Eclipse, IDE, for, programming]

4. Removing elements from the list

public boolean remove(Object obj)

This method is used to remove the specified object from the list.It returns false if the specified element doesn’t exist in the list.

It returns false if the specified element doesn’t exist in the list. If there are duplicate elements present in the list it removes the first occurrence of the specified element from the list.

Example :


package com.example;

import java.util.ArrayList;

public class ArrayListRemove {
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("D");
		al.add("E");
		al.add("F");
		System.out.println("ArrayList before remove:");
		for (String var : al) {
			System.out.println(var);
		}
		// Removing element A from the arraylist
		al.remove("A");
		// Removing element F from the arraylist
		al.remove("F");
		// Removing element C from the arraylist
		al.remove("C");
		/*
		 * This element is not present in the list so it should return false
		 */
		boolean b = al.remove("G");
		System.out.println("Element G removed: " + b);
		System.out.println("ArrayList After remove:");
		for (String var2 : al) {
			System.out.println(var2);
		}
	}
}

Output :


ArrayList before remove:
A
B
C
D
E
F
Element G removed: false
ArrayList After remove:
B
D
E

void clear()

This method will remove all the elements from the list.

protected void removeRange(int start, int end)

This method will remove all the elements starting from index start (included) until index end (not included).


5 . Searching elements in the List

public boolean contains(Object element)

ArrayList contains() method is used for checking the specified element existence in the given list.

It returns true if the specified element is found in the list else it will return false.

Example :


package com.example;

import java.util.ArrayList;

public class ArrayListContains {
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("D");

		System.out.println("ArrayList contains the string 'A': "
				+ al.contains("A"));
		System.out.println("ArrayList contains the string 'pen': "
				+ al.contains("pen"));
		System.out.println("ArrayList contains the string 'C': "
				+ al.contains("C"));
		System.out.println("ArrayList contains the string 'm': "
				+ al.contains("m"));

	}
}

Output :


ArrayList contains the string 'A': true
ArrayList contains the string 'pen': false
ArrayList contains the string 'C': true
ArrayList contains the string 'm': false

lastIndexOf(Object 0bj)

This method returns the index of last occurrence of the specified element in the ArrayList. It returns -1 if the specified element does not exist in the list.

When using lastIndexOf() it is important to test for -1. Exceptions may occur if we omit this test.

Syntax : public int lastIndexOf(Object obj)

Example :


package com.example;

import java.util.ArrayList;

public class ArrayListTest {
	public static void main(String[] args) {
		ArrayList<Integer> al = new ArrayList<Integer>();
		al.add(1);
		al.add(2);
		al.add(9);
		al.add(17);
		al.add(17);
		al.add(9);
		al.add(17);
		al.add(91);
		al.add(27);
		al.add(1);
		al.add(17);
		System.out
				.println("Last occurrence of element 1: " + al.lastIndexOf(1));
		System.out
				.println("Last occurrence of element 9: " + al.lastIndexOf(9));
		System.out.println("Last occurrence of element 17: "
				+ al.lastIndexOf(17));
		System.out.println("Last occurrence of element 91: "
				+ al.lastIndexOf(91));
		System.out.println("Last occurrence of element 88: "
				+ al.lastIndexOf(88));
	}
}

Output :

Last occurrence of element 1: 9
Last occurrence of element 9: 5
Last occurrence of element 17: 10
Last occurrence of element 91: 7
Last occurrence of element 88: -1

int indexOf(Object o)

Returns the index of the first occurrence of the specified element in the list. If this element is not in the list, the method returns -1.

Common Example :


package com.jwt.core;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListEX {

	public static void main(String args[]) {
		// Creating an empty array list
		ArrayList<String> list = new ArrayList<String>();

		// Adding elements to arrayList
		list.add("Item1");
		list.add("Item2");
		list.add(2, "Item3"); // it will add Item3 to the third position of
								// array list
		list.add("Item4");

		// Display the contents of the array list
		System.out.println("The arraylist contains the following elements: "
				+ list);

		// Checking index of an item
		int pos = list.indexOf("Item2");
		System.out.println("The index of Item2 is: " + pos);

		// Checking if array list is empty
		boolean check = list.isEmpty();
		System.out.println("Checking if the arraylist is empty: " + check);

		// Getting the size of the list
		int size = list.size();
		System.out.println("The size of the list is: " + size);

		// Checking if an element is included to the list
		boolean element = list.contains("Item5");
		System.out
				.println("Checking if the arraylist contains the object Item5: "
						+ element);

		// Getting the element in a specific position
		String item = list.get(0);
		System.out.println("The item is the index 0 is: " + item);

		// Retrieve elements from the arraylist

		// 1st way: loop using index and size list
		System.out
				.println("Retrieving items with loop using index and size list");
		for (int i = 0; i < list.size(); i++) {
			System.out.println("Index: " + i + " - Item: " + list.get(i));
		}

		// 2nd way:using foreach loop
		System.out.println("Retrieving items using foreach loop");
		for (String str : list) {
			System.out.println("Item is: " + str);
		}

		// 3rd way:using iterator
		System.out.println("Retrieving items using iterator");
		for (Iterator<String> it = list.iterator(); it.hasNext();) {
			System.out.println("Item is: " + it.next());
		}

		// Replacing an element
		list.set(1, "NewItem");
		System.out.println("The arraylist after the replacement is: " + list);

		// Removing items
		// removing the item in index 0
		list.remove(0);

		// removing the first occurrence of item "Item3"
		list.remove("Item3");

		System.out.println("The final contents of the arraylist are: " + list);

	}
}

Output :

The arraylist contains the following elements: [Item1, Item2, Item3, Item4]
The index of Item2 is: 1
Checking if the arraylist is empty: false
The size of the list is: 4
Checking if the arraylist contains the object Item5: false
The item is the index 0 is: Item1
Retrieving items with loop using index and size list
Index: 0 - Item: Item1
Index: 1 - Item: Item2
Index: 2 - Item: Item3
Index: 3 - Item: Item4
Retrieving items using foreach loop
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
Retrieving items using iterator
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
The arraylist after the replacement is: [Item1, NewItem, Item3, Item4]
The final contents of the arraylist are: [NewItem, Item4]
Previous Next Article

comments powered by Disqus