Saturday, August 30, 2025
HomeLanguagesJavaArrayList set() method in Java with Examples

ArrayList set() method in Java with Examples

The set() method of java.util.ArrayList class is used to replace the element at the specified position in this list with the specified element.

Syntax:

public E set(int index, E element)

Parameters: This method takes the following argument as a parameter.

  • index- index of the element to replace
  • element- element to be stored at the specified position

Returns Value: This method returns the element previously at the specified position.

Exception: This method throws IndexOutOfBoundsException if the index is not within the size range of the ArrayList.

Below are the examples to illustrate the set() method.

Example 1:




// Java program to demonstrate
// set() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of ArrayList<Integer>
            ArrayList<Integer>
                arrlist = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist.add(1);
            arrlist.add(2);
            arrlist.add(3);
            arrlist.add(4);
            arrlist.add(5);
  
            // print arrlist
            System.out.println("Before operation: "
                               + arrlist);
  
            // Replacing element at the index 3 with 30
            // using method set()
            int i = arrlist.set(3, 30);
  
            // Print the modified arrlist
            System.out.println("After operation: "
                               + arrlist);
  
            // Print the Replaced element
            System.out.println("Replaced element: "
                               + i);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown: "
                               + e);
        }
    }
}


Output:

Before operation : [1, 2, 3, 4, 5]
After operation : [1, 2, 3, 30, 5]
Replaced element : 4

Example 2: For IndexOutOfBoundsException




// Java program to demonstrate
// set() method
// for IndexOutOfBoundsException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of ArrayList<Integer>
            ArrayList<Integer>
                arrlist = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist.add(1);
            arrlist.add(2);
            arrlist.add(3);
            arrlist.add(4);
            arrlist.add(5);
  
            // print arrlist
            System.out.println("Before operation : "
                               + arrlist);
  
            // Replacing element at the index 7 with 30
            // using method set()
            System.out.println("\nTrying to Replace"
                               + " the element at"
                               + " (index > Capacity) ");
            int i = arrlist.set(7, 30);
  
            // Print the modified arrlist
            System.out.println("After operation: "
                               + arrlist);
  
            // Print the Replaced element
            System.out.println("Replaced element: "
                               + i);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Before operation : [1, 2, 3, 4, 5]

Trying to Replace the element at (index > Capacity) 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7, Size: 5
RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7013 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS