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
Throws :
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
Return value:
This method returns the element that was previously positioned at the given location.
Example 1 :
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]
Explanation :
Here we have first add the element in the list .Then after we have replace the value of array at indexes 3 , 4 and 5 with the element J , K and L respectively .
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)
Explanation :
Since the array only has indexes up to 5, we are attempting to replace the element at index 6 in the above example.As a result, following code execution, the compiler throws an exception for array index out of bond.
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
Login/Signup to comment