You tin role the set() method of java.util.ArrayList aeroplane to supersede an existing chemical ingredient of ArrayList inwards Java. The set(int index, due east element) method takes ii parameters, starting fourth dimension is the index of an chemical ingredient you lot desire to supersede together with minute is the novel value you lot desire to insert. You tin role this method equally long equally your ArrayList is non immutable e.g. non created using Collections.unmodifiableList(), inwards such instance the set() method throws java.lang.UnsupportedOperationExcepiton. Though, you lot tin equally good role the set() method alongside the List returned yesteryear Arrays.asList() method equally oppose to add() together with remove() which is non supported there. You only bespeak to endure careful alongside the index of elements. For example, if you lot desire to supersede the starting fourth dimension chemical ingredient so you lot bespeak to telephone phone set(0, newValue) because similar to an array, ArrayList index is equally good nix based.
Now, the questions come upwardly why exercise you lot desire to supersede an chemical ingredient inwards the ArrayList? Why non only take the chemical ingredient together with insert a novel one? Well, evidently the take together with insert volition possess got to a greater extent than fourth dimension than replace. The java.util.ArrayList provides O(1) fourth dimension performance for replacement, similar to size(), isEmpty(), get(), iterator(), together with listIterator() operations which runs inwards constant time.
Now, you lot may wonder that why set() gives O(1) performance but add() gives O(n) performance, because it could trigger resizing of array, which involves creating a novel array together with copying elements from former array to novel array. See Core Java Volume 1 - Fundamentals to acquire to a greater extent than nearly implementation together with working of ArrayList inwards Java.
Java Program to supersede elements inwards ArrayList
You tin encounter that initially, nosotros possess got a listing of five books together with nosotros possess got replaced the minute chemical ingredient yesteryear calling set(1, value) method, therefore inwards the output, the minute mass which was "Clean Coder" was replaced yesteryear "Introduction to Algorithms".
That's all about how to supersede existing elements of ArrayList inwards Java. The set() method is perfect to supersede existing value only brand certain that List you lot are using is non immutable. You tin equally good role this method alongside whatever other List type e.g. LinkedList. The fourth dimension complexity is O(n) because nosotros are doing index based access to the element.
Other ArrayList tutorials for Java Programmers
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures together with Algorithms: Deep Dive Using Java
Now, the questions come upwardly why exercise you lot desire to supersede an chemical ingredient inwards the ArrayList? Why non only take the chemical ingredient together with insert a novel one? Well, evidently the take together with insert volition possess got to a greater extent than fourth dimension than replace. The java.util.ArrayList provides O(1) fourth dimension performance for replacement, similar to size(), isEmpty(), get(), iterator(), together with listIterator() operations which runs inwards constant time.
Now, you lot may wonder that why set() gives O(1) performance but add() gives O(n) performance, because it could trigger resizing of array, which involves creating a novel array together with copying elements from former array to novel array. See Core Java Volume 1 - Fundamentals to acquire to a greater extent than nearly implementation together with working of ArrayList inwards Java.
Replacing an existing object inwards ArrayList
Here is an instance of replacing an existing value from ArrayList inwards Java. In this example, I possess got an ArrayList of String which contains names of some of the most pop together with useful books for Java programmers. Our instance replaces the s chemical ingredient of the ArrayList yesteryear calling the ArrayList.set(1, "Introduction to Algorithms") because the index of the array starts from zero. You should read a comprehensive mass similar "Big Java Early Object" yesteryear Cay S. Horstmann to acquire to a greater extent than nearly useful collection classes inwards Java, including ArrayList.Java Program to supersede elements inwards ArrayList
import java.util.ArrayList; import java.util.List; /* * Java Program to demonstrate how to supersede existing value inwards * ArrayList. */ public class ArrayListSetDemo { public static void main(String[] args) { // let's exercise a listing first List<String> top5Books = new ArrayList<String>(); top5Books.add("Clean Code"); top5Books.add("Clean Coder"); top5Books.add("Effective Java"); top5Books.add("Head First Java"); top5Books.add("Head First Design patterns"); // now, suppose you lot desire to supersede "Clean Coder" with // "Introduction to Algorithms" System.out.println("ArrayList earlier replace: " + top5Books); top5Books.set(1, "Introductoin to Algorithms"); System.out.println("ArrayList subsequently replace: " + top5Books); } } Output ArrayList earlier replace: [Clean Code, Clean Coder, Effective Java, Head First Java, Head First Design patterns] ArrayList subsequently replace: [Clean Code, Introduction to Algorithms, Effective Java, Head First Java, Head First Design patterns]
You tin encounter that initially, nosotros possess got a listing of five books together with nosotros possess got replaced the minute chemical ingredient yesteryear calling set(1, value) method, therefore inwards the output, the minute mass which was "Clean Coder" was replaced yesteryear "Introduction to Algorithms".
That's all about how to supersede existing elements of ArrayList inwards Java. The set() method is perfect to supersede existing value only brand certain that List you lot are using is non immutable. You tin equally good role this method alongside whatever other List type e.g. LinkedList. The fourth dimension complexity is O(n) because nosotros are doing index based access to the element.
Other ArrayList tutorials for Java Programmers
- How to take duplicate elements from ArrayList inwards Java? (tutorial)
- How to form an ArrayList inwards descending club inwards Java? (read)
- How to contrary an ArrayList inwards Java? (example)
- How to loop through an ArrayList inwards Java? (tutorial)
- How to synchronize an ArrayList inwards Java? (read)
- How to exercise together with initialize ArrayList inwards the same line? (example)
- Difference betwixt ArrayList together with HashSet inwards Java? (answer)
- Difference betwixt ArrayList together with HashMap inwards Java? (answer)
- Difference betwixt an Array together with ArrayList inwards Java? (answer)
- When to role ArrayList over LinkedList inwards Java? (answer)
- Difference betwixt ArrayList together with Vector inwards Java? (answer)
- How to acquire sublist from ArrayList inwards Java? (example)
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures together with Algorithms: Deep Dive Using Java
No comments:
Post a Comment