ArrayList Methods

In this article, we will discuss some basic operations that can be performed on an ArrayList in Java. ArrayList is a dynamic array that is present in the Java Collection Framework.

Adding Elements: To add an element to an ArrayList, we can use the add() method. This method can perform multiple operations based on different parameters. The first is add(Object), which adds an element to the end of the ArrayList. The second is add(int index, Object), which adds an element at a specific index in the ArrayList and moves all the previously present elements from that index one position ahead.

Let's take a look at an example of how to add elements to an ArrayList in Java:

java
import java.util.*; public class Example { public static void main(String args[]) { ArrayList<String> al = new ArrayList<>(); al.add("Geeks"); al.add("Geeks"); al.add(1, "For"); System.out.println(al); } } Output: [Geeks, For, Geeks]

Checking if an element is present: The ArrayList class provides a method called contains() that takes an object of type ArrayList element as a parameter and checks if it is present in the ArrayList or not. It returns a boolean value true or false based on if the object is present or not.

Removing Elements: To remove an element from an ArrayList, we can use the remove() method. This method can also perform multiple operations based on different parameters. The first is remove(Object), which removes an object from the ArrayList. If there are multiple such objects, then the first occurrence of the object is removed. The second is remove(int index), which takes an integer value that simply removes the element present at that specific index in the ArrayList. After removing the element, all the elements are moved to the left to fill the space, and the indices of the objects are updated.

Let's take a look at an example of how to remove elements from an ArrayList in Java:

java
import java.util.*; class Example { public static void main (String[] args) { // Create an ArrayList ArrayList<String> al = new ArrayList<String>(); al.add("geeks"); al.add("ide"); al.add("courses"); System.out.println(al.contains("ide")); al.remove(1); System.out.println(al.contains("ide")); al.remove("courses"); System.out.println(al.contains("courses")); } } Output: true false false

Let's discuss some more methods of ArrayList:

  • get(index): The get() method of ArrayList in Java is used to get the element of a specified index within the list. It throws an IndexOutOfBounds exception if the index is not within the range of ArrayList.
  • set(index, Object): The set() method of the java.util.ArrayList class is used to replace the element at the specified position in this list with the specified element.
  • indexOf(Object): The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list or -1 if this list does not contain the element.
  • lastIndexOf(Object): The lastIndexOf() method of ArrayList returns the index of the last occurrence of the specified element in this list or -1 if this list does not contain the element.

Let's take a look at an example of how to use the above four methods in Java:

java
import java.util.*; class Example { public static void main (String[] args) { // Create an ArrayList ArrayList<Integer> al = new ArrayList<Integer>(); al.add(10); al.add(20); al.add(10); al.add(30); System.out.println(al.get(1)); 
        al.set(index,40);
System.out.println(al.get(1));
System.out.println(al.indexOf(10));
System.out.println(al.lastIndexOf(10));
System.out.println(al.indexOf(50));

Output:

20 40 0 2 -1

The method clear() is another important method available in ArrayList that can be used to remove all elements from the list. It returns void and does not return anything. Here is an example:

java
import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(10); al.add(20); al.add(10); al.add(30); System.out.println(al.isEmpty()); // false al.clear(); System.out.println(al.isEmpty()); // true } }

The time complexity of the add() method is amortized O(1), which means that in the worst case, it could take O(n) time to add an element to the list, but on average, it takes O(1) time. The size() method and the isEmpty() method both have a time complexity of O(1). The get() and set() methods have a time complexity of O(1) when accessing elements by their index. The contains() and indexOf() methods have a time complexity of O(n), while the lastIndexOf() method has a worst-case time complexity of O(n) because it may need to traverse the entire list to find the last occurrence of an element. The remove() method has a worst-case time complexity of O(n) because it may need to shift elements in the list to fill the gap left by the removed element. The add(int index, Object obj) method also has a worst-case time complexity of O(n) because it may need to shift elements in the list to make room for the inserted element.

Previous Post Next Post