Java ArrayList indexOf() lastIndexOf() isEmpty() Methods Tutorial

In this section, we will learn what the ArrayList indexOf(), lastIndexOf(), and isEmpty() methods are and how to use them in Java.

What is Java ArrayList indexOf() Method?

The Java ArryayList indexOf() method is used to search for the index number of an element in a list object.

For example, let’s say you have a list of integer numbers and want to know if there’s the value 100 in that list and if yes, get its index number. Well, using this method, you can exactly do that.

Note: if there are multiple same values in a list object, this method will return the index of the first occurrence.

Java ArrayList indexOf() Method Syntax:

int indexOf(Object o)

indexOf() Method Parameters:

The method takes one argument, and that is the element we want to check in a list object to find its index number.

indexOf() Method Return Value:

The return value of this method is of type integer and is equal to the index number of the element we set as the argument of this method.

Note: if the element does not exist on the target list, the return value will be -1.

indexOf() Method Exceptions:

ClassCastException: You’ll get this exception if the argument element is of type that is incompatible with the type of the elements in the target list object.

NullPointerException: this exception will be thrown if the argument of the method is null.

Example: using ArrayList indexOf() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        System.out.println("The index of the value four is: "+ list.indexOf("Four"));
    }
}

Output:

The index of the value four is: 3

What is Java ArrayList lastIndexOf() Method?

The Java ArryayList lastIndexOf() method is used to search for the index number of a value in a list object. But this method runs its search from the end of a list object and moves toward the beginning. For this reason, it will return the index number of the last occurrence of the target value.

This means if in a list object there are 3 occurrences of the value “Ten” for example, using this method you get the index number of the last “Ten” value.

Java ArrayList lastIndexOf() Method Syntax:

int lastIndexOf(Object o)

lastIndexOf() Method Parameters:

The method takes one argument and that is the element we want to search for in a list object.

lastIndexOf() Method Return Value:

The return value of this method is the index number of the last occurrence of the target element we’ve set as the argument of this method.

Note: if the element wasn’t in the target list object, you’ll get the value -1 as a result.

lastIndexOf() Method Exceptions:

This method throws the same exceptions that the indexOf() method might throw.

Example: using ArrayList lastIndexOf() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        System.out.println("The index of last occurrence of the value Six is: "+ list.lastIndexOf("Six"));
    }
}

Output:

The index of last occurrence of the value Six is: 6

In this example, there are two occurrences of the value “Six” on the list. Now when we called the lastIndexOf() method on this list to get the index number of last occurrence of the value “Six”, it returned the index number 6. So basically it ignored the first occurrence and moved to the last one.

What is Java ArrayList isEmpty() Method?

The Java ArryayList isEmpty() method is used to check a list object and see if it is empty or not.

When we say empty, it means if there are one or more elements in the target list object.

Java ArrayList isEmpty() Method Syntax:

boolean isEmpty()

isEmpty() Method Parameters:

The method does not take an argument.

isEmpty() Method Return Value:

The return value of this method is a Boolean value:

If the target list object was empty, the return value of this method is true.

Otherwise, the return value will be false.

isEmpty() Method Exceptions:

The method does not throw an exception.

Example: using ArrayList isEmpty() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        ArrayList<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        if (list.isEmpty()){
            System.out.println("Yes the list is empty");
        }else{
            System.out.println("No the list is not empty");
        }
    }
}

Output:

No the list is not empty

In this example, the target list is not empty and so calling the `isEmpty()` method on it returns the value false. That’s why the body of the else statement ran instead of the if statement’s body.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies