.set()
In Java, the .set()
method replaces the element present at a specified position with another element in an ArrayList
. After execution, the method returns the replaced element.
Understanding how .set()
works is crucial when dealing with mutable lists in Java, especially when updating or modifying elements during processing.
Syntax
arrayList.set(index, element);
Parameters:
index
: The position in the list where the element should be replaced.element
: The new element that will replace the old one at the given index.
Return value:
The .set()
method returns the replaced element.
Note: The
element
must be of the same data type as the rest of the elements in thearrayList
. Otherwise, an error will occur.
Example 1: Basic Usage of .set()
This example uses the .set()
method to replace elements at multiple positions in an ArrayList
:
import java.util.ArrayList;public class Students {public static void main(String[] args) {// Create an ArrayListArrayList<String> studentList = new ArrayList<String>();// Add values to the ArrayListstudentList.add("John");studentList.add("Lily");studentList.add("Samantha");studentList.add("Tony");// Replace elements at multiple positions in the ArrayListString replacedStudentOne = studentList.set(1, "David");String replacedStudentTwo = studentList.set(2, "George");// Print the updated ArrayListSystem.out.println("Updated ArrayList: " + studentList);}}
Here is the output:
Updated ArrayList: [John, David, George, Tony]
Example 2: Using .set()
with Integers
This example uses the .set()
method to replace an integer with another integer in an ArrayList
:
import java.util.ArrayList;public class Example2 {public static void main(String[] args) {// Create an ArrayListArrayList<Integer> numbers = new ArrayList<>();// Add values to the ArrayListnumbers.add(10);numbers.add(20);numbers.add(30);// Replace the second elementnumbers.set(1, 99);// Print the updated ArrayListSystem.out.println(numbers);}}
Here is the output:
[10, 99, 30]
Example 3: Handling Index Errors
This example attempts to replace an element at an out-of-bounds index using the .set()
method, resulting in an IndexOutOfBoundsException
:
import java.util.ArrayList;public class Example3 {public static void main(String[] args) {// Create an ArrayListArrayList<String> colors = new ArrayList<>();// Add values to the ArrayListcolors.add("Red");colors.add("Green");try {colors.set(5, "Blue");} catch (IndexOutOfBoundsException e) {System.out.println("Error: " + e.getMessage());}}}
In this example, the try...catch
block is used to catch the exception and display the error message.
Here is the output:
Error: Index 5 out of bounds for length 2
Frequently Asked Questions
1. Can .set()
be used to add new elements to a list?
No. The .set()
method replaces an existing element at a given index. To insert a new element, use .add()
instead.
2. Is .set()
available for all collections in Java?
No. .set()
is only available for classes that implement the List
interface, such as ArrayList
, LinkedList
, and Vector
.
3. Does .set()
change the size of the list?
No. .set()
only modifies the value at the specified index.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours