Easy Tutorial
❮ Java Data Structures File Delete ❯

Java ArrayList set() Method

Java ArrayList

The set() method is used to replace an element at a specified index in a dynamic array.

The syntax for the set() method is:

arraylist.set(int index, E element)

Note: arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

Returns the element previously at the specified position.

If the index value is out of range, it throws an IndexOutOfBoundsException.

Example

Replacing an element in a dynamic array:

Example

import java.util.ArrayList;

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

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        System.out.println("Website List: " + sites);

        // Element at index 2 is replaced
        String element = sites.set(2, "Wiki");
        System.out.println("After Replacement: " + sites);
        System.out.println("Replaced Element: " + element);
    }
}

Executing the above program outputs:

After Replacement: [Google, tutorialpro, Wiki]
Replaced Element: Taobao

In the above example, we created an array named sites, and the set() method replaced "Taobao" at index position 2 with "Wiki".

Note: If the index value is uncertain, you can use the ArrayList indexOf() method.

ArrayList set() vs add() Method

The syntax for both add() and set() methods looks very similar.

// Syntax for add()
arraylist.add(int index, E element)
// Syntax for set()
arraylist.set(int index, E element)

Both methods add a new element to the array.

However, they have significant differences:

Example

import java.util.ArrayList;

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

        // Create a dynamic array
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("tutorialpro");
        sites.add("Taobao");
        System.out.println("Website List: " + sites);

        // Create another dynamic array sites2
        ArrayList<String> sites2 = new ArrayList<>();

        // Add elements from sites to sites2
        sites2.addAll(sites);
        System.out.println("ArrayList: " + sites);

        // Use set()
        sites.set(1, "Wiki");
        System.out.println("ArrayList after using set(): " + sites);

        // Use add()
        sites2.add(1, "Wiki");
        System.out.println("ArrayList after using add(): " + sites2);
    }
}

Executing the above program outputs:

Website List: [Google, tutorialpro, Taobao]
ArrayList after using set(): [Google, Wiki, Taobao]
ArrayList after using add(): [Google, Wiki, tutorialpro, Taobao]

ArrayList: [Google, tutorialpro, Taobao] ArrayList after using set(): [Google, Wiki, Taobao] ArrayList after using add(): [Google, Wiki, tutorialpro, Taobao]

In the example above, we created two dynamic arrays named sites and sites2. We used the ArrayList addAll() method to make both dynamic arrays have the same elements.

The set() method replaces the element at index position 1.

The add() method adds the Wiki element at index 1, changing the index of the tutorialpro element to 2.

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

Java ArrayList

❮ Java Data Structures File Delete ❯