Java List get() set() Methods Tutorial

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

What is Java List get() Method?

The Java List get() method is used to get an element of a list by giving its index number.

For example, if you want to get the element in the index number 4 of a list object, we put this value as the argument of this method and as a result, the element in the target index will return.

Java List get() Method Syntax:

public E get(int index);

get() Method Parameters:

This method takes only one argument, and that is the index number of the element we want to get in a list object.

get() Method Return Value:

The return value of this method is an element that we’ve specified 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 List 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 List set() Method?

The Java List set() method is used to set a value for a list object at a specific index.

For example, if there’s a list object with 10 elements and you want to replace the element at index number 3 with another value, you can then use this method.

Note: using this method will replace the current element at the specified index number.

Java List set() Method Syntax:

E set( int index, E element);

set() Method Parameters:

The method takes two arguments:

  • The first argument is the index number where we want to replace the current element there with a new one.
  • The second argument is the new element that we want to replace with the old one.

set() Method Return Value:

The return value of this method is the old element that was replaced 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 List 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
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies