Arraylist set method – Java ArrayList set() Method with Example

Arraylist set method: In the previous article we have discussed about Java ArrayList clone() Method with Example

In this article we are going to see the use Java ArrayList set() method along with suitable examples.

Java ArrayList set() Method with Example

set():

Java arraylist set: This java.util.ArrayList.set() method is used to replace the present element to a specific element in a specified position within the arraylist.

It returns true if the index is present within the range or the size of the ArrayList else it throws IndexOutOfBoundException. Also If the ArrayList is of one type, and we try to replace the existing element with another type then it shows ClassCastException.

Syntax:

arrayListName.set(int index, Object element)

Where,

  • arrayListName refers to the name of your ArrayList.
  • int index refers to the specified index position of whose element is to be replaced with the new element.
  • Object element refers to the specific element that will replace the existing element of the arrayList.

Let’s see different examples to understand it more clearly.

Method-1: Java ArrayList set() Method – Example with String Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add string elements into the ArrayList using the add() method.
  • Display the ArrayList elements.
  • Then replace the existing element with a new element in the arrayList using set() method.
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create an ArrayList of string datatype
        ArrayList<String> arr = new ArrayList<String>();
        // Adding some elements to the ArrayList
        arr.add("Hello");
        arr.add("this");
        arr.add("is");
        arr.add("an");
        arr.add("ArrayList");
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // setting  old element to new element from the ArrayList using set(int index, Object o)
        arr.set(4,"example of set() Method");
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [Hello, this, is, an, ArrayList]
The new elements of ArrayList are: [Hello, this, is, an, example of set() Method]

Method-2: Java ArrayList set() Method – Example with Integer Type ArrayList

Approach:

  • Create a new ArrayList of type String.
  • Add Integer elements into the ArrayList using the add() method.
  • Display the ArrayList elements
  • Then replace the existing element with a new element in the arrayList using set() method.
  • Print the new ArrayList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create an ArrayList of Integer datatype
        ArrayList<Integer> arr = new ArrayList<Integer>();
        // Adding some elements to the ArrayList
        arr.add(2);
        arr.add(52);
        arr.add(13);
        arr.add(17);
        arr.add(1);
        // Prints the ArrayList elements
        System.out.println("The elements of ArrayList are: "+arr);
        // setting  old element to new element from the ArrayList using set(int index, Object o)
        arr.set(4,99);
        // Prints the new ArrayList elements
        System.out.println("The new elements of ArrayList are: "+arr);
    }
}
Output:

The elements of ArrayList are: [2, 52, 13, 17, 1]
The new elements of ArrayList are: [2, 52, 13, 17, 99]

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: