Java ArrayList get() set() Methods Tutorial

In this section we will learn what the ArrayList get() and set() methods are and how to use them in Java.

What is Java ArrayList get() Method?

The Java ArrayList get() method is used to get an element in an ArrayList using its index number.

Basically, if there’s an ArrayList object and you want to get the value in a specific index number, then you can use the get() method.

Java ArrayList get() Method Syntax:

public E get(int index);

get() Method Parameters:

The method takes one argument and that is the index number that we want to get the element stored there.

get() Method Return Value:

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

get() Method Exceptions:

This method throws `IndexOutOfBoundException` if we set an index number that is beyond the limit of the target list object.

For example, if we set the value 10 for a list object that has only 5 elements, this exception will be thrown.

Example: using ArrayList get() method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        System.out.println("The value at index 4 is: "+ list.get(4));
    }
}

Output:

The value at index 4 is: 5

What is Java ArrayList set() Method?

The Java ArrayList set() method is used to replace one of the current element of an ArrayList object with a new value.

Basically, using this method, we can manipulate the current elements of an ArrayList object and replace them with new values.

Java ArrayList set() Method Syntax:

E set( int index, E element);

set() Method Parameters:

This method has two parameters:

  • The first parameter is the index number where we want to replace its element with a new one.
  • The second parameter is a new value that we want to replace the old value with.

set() Method Return Value:

The return value of this method is the old value that we’ve just replaced it with a new one.

set() Method Exceptions:

This method might throw a couple of exceptions:

UnsupportedOperationException: This exception is thrown if the target object list does not support the set() method.

ClassCastException: This exception will be thrown if the new element is of different type and cannot be cast to the right datatype.

NullPointerException: This exception will be thrown if we attempt to put the value `null` as the replacement value (the second argument of the method).

IllegalArgumentException: This exception will be thrown if a property of the new element prevents it from being added to the list object.

IndexOutOfBoundsException: This exception will be thrown if we attempt to put the new element in an index number that is out of range of the target list object.

Example: using ArrayList set() method

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

public class Main {

    public static void main(String[] args){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        System.out.println("The old value at index 4 is: "+ list.get(4));
        list.set(4,200);
        System.out.println("The new value at index 4 is: "+ list.get(4));
    }
}

Output:

The old value at index 4 is: 5

The new value at index 4 is: 200

Leave a Reply