Java ArrayList is one of the most commonly used data structures. Through this, app developers can add or remove elements at runtime. Specifically, Java ArrayList Set() method replaces the selected element at a specified index with a new one.
Table of Contents
In this article, we’ll describe how to use the set() method in ArrayList with examples.
The function of ArrayList in Java is clear. Below is the syntax for the set() method in Java:
public E set(int index, E element)
Here, “E” represents the type of elements that the Java ArrayList can hold. In plain words, the set() method works with two parameters. The first represents the index of the selected replaceable element, while the second represents the newly added element.
Let’s create a Java ArrayList Set of type String and add some elements. Then, we will use the set() method to replace an element at a specific index with a new element.
import java.util.ArrayList; public class ArrayListSetExample { public static void main(String[] args) { ArrayList<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Banana"); fruits.add("Grapes"); System.out.println("Original ArrayList: " + fruits); fruits.set(1, "Pineapple"); System.out.println("ArrayList after set operation: " + fruits); } }
In the above example, we created an ArrayList of type String and added four elements. Then, we printed the original ArrayList and used the set() method to replace the element at index 1 with “Pineapple.” Finally, we processed the updated ArrayList.
Original ArrayList: [Apple, Mango, Banana, Grapes]
ArrayList after set operation: [Apple, Pineapple, Banana, Grapes]
In this example, we will create an ArrayList of type Integer and add some elements. Then, we will take user input to replace an element at a specific index with a new element.
import java.util.ArrayList; import java.util.Scanner; public class ArrayListSetExample2 { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of elements in the ArrayList: "); int n = scanner.nextInt(); for(int i=0;i<n;i++) { System.out.print("Enter the element at index " + i + ": "); int num = scanner.nextInt(); numbers.add(num); } System.out.println("Original ArrayList: " + numbers); System.out.print("Enter the index of the element to replace: "); int index = scanner.nextInt(); System.out.print("Enter the new element: "); int element = scanner.nextInt(); numbers.set(index, element); System.out.println("ArrayList after set operation: " + numbers); scanner.close(); } }
In the above example, we created an ArrayList of type Integer and added elements based on user input. Then, we printed the original Java ArrayList and took user input to replace an element at a specific index with a new one.
Enter the element at index 0: 5 Enter the element at index 1: 10 Enter the element at index 2: 15 Original ArrayList: [5, 10, 15] Enter the index of the element to replace: 1 Enter the new element: 7 ArrayList after set operation: [5, 7, 15]
1. Dynamic Size: The size of an ArrayList is easy to modify, and the developer can adjust the size with the addition or removal of elements. Unlike arrays, ArrayList does not require memory pre-allocation for a fixed number of elements.
2. Random Access: ArrayList provides fast access to elements based on their index. The feature enables efficient searching and retrieval of elements, making it ideal for use cases where quick access is mandatory.
3. Automatic Resizing: At initialization, a Java ArrayList has a default size. The size auto-adjusts and increase or decrease with the addition or removal of elements. Thanks to this, the developers don’t have to resize lists manually.
4. Convenient Methods: ArrayList provides a variety of methods for manipulating elements, including the feature to add, remove, and replace. The ArrayList Set Java method allows developers to easily update the elements’ value at a specific index in the ArrayList.
5. High Compatibility: ArrayList implements the List interface, making it compatible with other Java collections. Through this, Java developers can use ArrayList alongside others, such as LinkedList, HashSet, or TreeMap.
6. Improved Memory Management: ArrayList is auto-optimized for memory management. On removal of elements from the ArrayList, the memory releases automatically. The added feature reduces the risk of memory leaks and helps to improve application performance.
In short, the ArrayList set method offers several advantages to simplify data structure use. Its dynamic size, random access, automatic resizing, and convenient methods make it ideal for use cases where quick access to elements and efficient list manipulation are priorities.
Read Also: Functionalities & Uses of Java Stream Collectors
Create a free profile and find your next great opportunity.
Sign up and find a perfect match for your team.
Xperti vets skilled professionals with its unique talent-matching process.
Connect and engage with technology enthusiasts.
© Xperti.io All Rights Reserved
Privacy
Terms of use