English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Java ArrayList set() method replaces the element at the specified position with the specified element from the arraylist.
The syntax of set() method is:
arraylist.set(int index, E element)
The set() method has two parameters.
index - Position of the replaced element
element - New element to be stored at index
Returns the element previously at the index
NoteIf the specified index is out of range, the set() method will throw IndexOutOfBoundsException.
import java.util.ArrayList; class Main { public static void main(String[] args) { //Create ArrayList ArrayList<String> languages = new ArrayList<>(); //Add elements to ArrayList languages.add("Python"); languages.add("English"); languages.add("JavaScript"); System.out.println("ArrayList: " + languages); //replacement index1element at the position String element = languages.set(1, "Java"); System.out.println("Updated ArrayList: ", + languages); System.out.println("The replaced element: ", + element); } }
Output Result
ArrayList: [Python, English, JavaScript] Updated ArrayList: [Python, Java, JavaScript] element being replaced: English
In the above example, we created an ArrayList named languages. Here, we use the set() method to replace the index1(English) element to replace Java.
Note:If you are not sure of the index number of an element, you can useArrayList indexOf()methods.
The syntax of add() and set() methods looks very similar.
// add() syntax arraylist.add(int index, E element) // set() syntax arraylist.set(int index, E element)
And, both methods add a new element to the arraylist. This is why some people think these two methods are similar.
However, there are major differences between them.
The set() method is used to: Add a new element at the specified position by replacing the old element at the specified position.
The add() method is used to: Add a new element at the specified position by moving the old element to the correct position.
import java.util.ArrayList; class Main { public static void main(String[] args) { //Create ArrayList ArrayList<String> languages1 = new ArrayList<>(); //Add elements to ArrayList languages1.add("Python"); languages1.add("English"); languages1.add("JavaScript"); //create another one like language1similar ArrayList ArrayList<String> languages2 = new ArrayList<>(); //Set languages1add all elements in languages2in languages2.addAll(languages1); System.out.println("ArrayList: " + languages1); //Using the set() method languages1.set(1, "Java"); System.out.println("set() after the ArrayList: " + languages1); //Using the add() method languages2.add(1, "Java"); System.out.println("add() after the ArrayList: " + languages2); } }
Output Result
ArrayList: [Python, English, JavaScript] ArrayList after set(): [Python, Java, JavaScript] ArrayList after add(): [Python, Java, English, JavaScript]
In the above example, we created two arraylists named languages1and languages2of the ArrayList. We usedArrayList addAll()methods, so that the two arraylists have the same elements.
Here,
The set() method replaces the element at the position1The 'English' element at the position
The add() method moves the element 'English' to the position2
Can be accessedJava ArrayList add()to learn more information.