Java ArrayList set() Method

Java ArrayList Set() Method

What is ArrayList in Java ?

Arraylist is a part of collection framework in java. It contains several inbuilt function like set() method which can be found in the java.util.ArrayList Package. It implements the List interface of the collections framework.

Java ArrayList set() Method 

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

Syntax  :

public E set(int index, E element)

Parameters : 

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

Return value:

This method returns the element that was previously positioned at the given location.
set() method java

Example 1 :  

Run

import java.util.ArrayList;
public class Main{
    public static void main(String[] args) {

        ArrayList list = new ArrayList<>();

        //Adding elements to the list
        list.add("A");      
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");

        //Printing the ArrayList elements before replacing.
        System.out.println(list);

        //Replacing  the elements at the specified position of ArrayList.
        list.set(3, "J");
        list.set(4, "K");
        list.set(5, "L");

        //Printing the ArrayList elements after replacing.
        System.out.println(list);
    }

}

Output :

[A, B, C, D, E, F]
[A, B, C, J, K, L]

Example 2 :

Run
import java.util.ArrayList;
public class Main{
    public static void main(String[] args) {

        ArrayList list = new ArrayList<>();

        //Adding elements to the list
        list.add("A");      
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");

        //Printing the elements before replacing.
        System.out.println(list);

        //Trying to replace the element at index 6
        list.set(6, "I");
      
        
    }

}

Output :

[A, B, C, D, E, F]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 6 out of bounds for length 6
        at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:100)
        at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
        at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
        at java.base/java.util.Objects.checkIndex(Objects.java:385)
        at java.base/java.util.ArrayList.set(ArrayList.java:441)
        at ArrayListSet.main(ArrayListSet.java:19)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription