Java ArrayList set()

The syntax of the set() method is:

arraylist.set(int index, E element)

Here, arraylist is an object of the ArrayList class.


set() Parameters

The set() method takes two parameters.

  • index - position of the element to be replaced
  • element - new element that is to be stored at index

set() Return Values

  • returns the element previously present at index
  • throws IndexOutOfBoundsException, if index is out of range

Example 1: Replace an Element in ArrayList

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {

    // create an ArrayList
    ArrayList<String> languages = new ArrayList<>();

    // add elements to ArrayList
    languages.add("Python");
    languages.add("English");
    languages.add("JavaScript");
    System.out.println("ArrayList: " + languages);

    // replace element at index 1
    String element = languages.set(1, "Java");
    System.out.println("Updated ArrayList: " + languages);
    System.out.println("Replaced Element: " + element);
  }
}

Output

ArrayList: [Python, English, JavaScript]
Updated ArrayList: [Python, Java, JavaScript]
Replaced Element: English

In the above example, we have created an arraylist named languages. Here, we have used the set() method to replace the element at index 1 (English) with Java.

Note: If you are not sure about the index number of an element, you can use the ArrayList indexOf() method.


ArrayList set() Vs. add()

The syntax of the add() and set() method looks quite similar.

// syntax of add()
arraylist.add(int index, E element)

// syntax of set()
arraylist.set(int index, E element)

And, both the methods are adding a new element to the arraylist. This is why some people consider both methods similar.

However, there is a major difference between them.

  • The set() method adds a new element at the specified position by replacing the older element at that position.
  • The add() method adds a new element at the specified position by shifting the older element towards the right position.

Example 2: ArrayList set() Vs. add()

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {

    // create an ArrayList
    ArrayList<String> languages1 = new ArrayList<>();

    // add elements to ArrayList
    languages1.add("Python");
    languages1.add("English");
    languages1.add("JavaScript");

    // create another ArrayList similar to languages1
    ArrayList<String> languages2 = new ArrayList<>();

    // adds all elements from languages1 to languages2
    languages2.addAll(languages1);
    System.out.println("ArrayList: " + languages1);

    // use of set()
    languages1.set(1, "Java");
    System.out.println("ArrayList after set(): " + languages1);

    // use of add()
    languages2.add(1, "Java");
    System.out.println("ArrayList after add(): " + languages2);

  }
}

Output

ArrayList: [Python, English, JavaScript]
ArrayList after set(): [Python, Java, JavaScript]
ArrayList after add(): [Python, Java, English, JavaScript]

In the above example, we have created two arraylists named languages1 and languages2. We have used the ArrayList addAll() method so that both arraylists have the same elements.

Here,

  • the set() method replaces the element English at position 1
  • the add() method shifts the element English to position 2

To learn more about adding value, visit Java ArrayList add().

Did you find this article helpful?