Java ArrayList set() Method
The set() method is used to replace the element at the specified index in the dynamic array.
The syntax of the set() method is:
arraylist.set(int index, E element)
Note:arraylist is an object of the ArrayList class.
Parameter Details:
- index - index position
- element - the new element to replace at the index position
Return Value
Returns the element previously at the index position.
If the index value is out of range, an IndexOutOfBoundsException is thrown.
Example
Replace an element in the dynamic array:
Example
class Main {
public static void main(String[] args){
// Create a dynamic array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
System.out.println("Website list: " + sites);
// The 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 produces the following output:
替换后: [Google, Example, Wiki] 被替换的元素: 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 you are not sure about the index value of an element, you can use the ArrayList indexOf() method.
ArrayList set() vs add() Method
The syntax of the add() and set() methods looks very similar.
// add() 的语法 arraylist.add(int index, E element) // set() 的语法 arraylist.set(int index, E element)
Both methods add a new element to the array.
However, there is a big difference between them:
- The set() method updates the element at the specified position.
- The add() method inserts an element into the dynamic array at the specified position.
Example
class Main {
public static void main(String[] args){
// Create a dynamic array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Example");
sites.add("Taobao");
System.out.println("Website list: " + sites);
// Create another dynamic array sites2
ArrayList<String> sites2 = new ArrayList<>();
// Add the elements of sites to sites2
sites2.addAll(sites);
System.out.println("ArrayList: " + sites);
// Using set()
sites.set(1, "Wiki");
System.out.println("ArrayList after using set(): " + sites);
// Using add()
sites2.add(1, "Wiki");
System.out.println("ArrayList after using add(): " + sites2);
}
}
Executing the above program produces the following output:
网站列表: [Google, Example, Taobao] ArrayList: [Google, Example, Taobao] ArrayList 使用 set() 后: [Google, Wiki, Taobao] ArrayList 使用 add() 后: [Google, Wiki, Example, Taobao]
In the above example, we created two dynamic arrays named sites and sites2. We used the ArrayList addAll() method so that the two 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 position 1, changing the index of the element Example to 2.
To learn more about adding values, visitJava ArrayList add()。
Other Extensions
Java ArrayList