Replace existing element in ArrayListTo replace an existing element, we must find the exact position (index) of the element in arraylist. Once we have the index, we can use set() method to update the replace the old element with new element. Show
ArrayList set() method exampleJava program to update arraylist object and set value at index. public class ArrayListExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); System.out.println(list); //Replace C with C_NEW //1 - In multiple steps int index = list.indexOf("C"); list.set(index, "C_NEW"); System.out.println(list); //2 - In single step replace D with D_NEW list.set( list.indexOf("D") , "D_NEW"); System.out.println(list); } }Program output. [A, B, C, D] [A, B, C_NEW, D] [A, B, C_NEW, D_NEW]Happy Learning !! Read More: A Guide to Java ArrayList Was this post helpful?Let us know if you liked the post. That’s the only way we can improve. Yes No Further Reading:
Java.util.ArrayList.add() Method in Java
Below are the add() methods of ArrayList in Java:
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Article Tags : Java Java - util package Java-ArrayList Java-Collections Java-Functions Practice Tags : Java Java-Collections List Interface in Java with Examples
The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The List interface is found in java.util package and inherits the Collection interface. It is a factory of ListIterator interface. Through the ListIterator, we can iterate the list in forward and backward directions. The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector. ArrayList and LinkedList are widely used in Java programming. The Vector class is deprecated since Java 5. Declaration: The List interface is declared as: public interface List<E> extends Collection<E>;Let us elaborate on creating objects or instances in a List class. Since List is an interface, objects cannot be created of the type list. We always need a class that implements this List in order to create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the List. Just like several other user-defined ‘interfaces’ implemented by user-defined ‘classes’, List is an ‘interface’, implemented by the ArrayList class, pre-defined in the java.util package. Syntax: This type of safelist can be defined as: List<Obj> list = new ArrayList<Obj> ();
Example: Java
Output [1, 2] [1, 1, 2, 3, 2] [1, 2, 3, 2] 2 [5, 2, 3, 2] Now let us perform various operations using List Interface to have a better understanding of the same. We will be discussing the following operations listed below and later on implementing via clean java codes. Operations in a List interfaceSince List is an interface, it can be used only with a class that implements this interface. Now, let’s see how to perform a few frequently used operations on the List.
Now let us discuss the operations individually and implement the same in the code to grasp a better grip over it. Operation 1: Adding elements to List class using add() method In order to add an element to the list, we can use the add() method. This method is overloaded to perform multiple operations based on different parameters. Parameters: It takes 2 parameters, namely:
Example: Java
Output [Geeks, For, Geeks] Operation 2: Updating elements After adding the elements, if we wish to change the element, it can be done using the set() method. Since List is indexed, the element which we wish to change is referenced by the index of the element. Therefore, this method takes an index and the updated element which needs to be inserted at that index. Example: Java
Output Initial ArrayList [Geeks, Geeks, Geeks] Updated ArrayList [Geeks, For, Geeks] Operation 3: Removing Elements In order to remove an element from a list, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters. They are: Parameters:
Example: Java
Output Initial ArrayList [Geeks, For, Geeks] After the Index Removal [Geeks, Geeks] After the Object Removal [Geeks] Iterating over ListTill now we are having a very small input size and we are doing operations manually for every entity. Now let us discuss various ways by which we can iterate over the list to get them working for a larger sample set. Methods: There are multiple ways to iterate through the List. The most famous ways are by using the basic for loop in combination with a get() method to get the element at a specific index and the advanced for a loop. Example: Java
Output Geeks For Geeks Geeks For Geeks Methods of the List InterfaceSince the main concept behind the different types of the lists is the same, the list interface contains the following methods:
Java List vs SetBoth the List interface and the Set interface inherits the Collection interface. However, there exists some differences between them.
Classes Association with a List InterfaceNow let us discuss the classes that implement the List Interface for which first do refer to the pictorial representation below to have a better understanding of the List interface. It is as follows: AbstractList, CopyOnWriteArrayList, and the AbstractSequentialList are the classes that implement the List interface. A separate functionality is implemented in each of the mentioned classes. They are as follows:
We will proceed in this manner.
Let us discuss them sequentially and implement the same to figure out the working of the classes with the List interface. Class 1: ArrayListAn ArrayList class which is implemented in the collection framework provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. Let’s see how to create a list object using this class. Example: Java
Output [1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5 Class 2: VectorVector is a class that is implemented in the collection framework implements a growable array of objects. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. Vectors basically fall in legacy classes but now it is fully compatible with collections. Let’s see how to create a list object using this class. Example: Java
Output [1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5 Class 3: StackStack is a class that is implemented in the collection framework and extends the vector class models and implements the Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search and peek. Let’s see how to create a list object using this class. Example: Java
Output [1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5 Class 4: LinkedListLinkedList is a class that is implemented in the collection framework which inherently implements the linked list data structure. It is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. Let’s see how to create a list object using this class. Example: Java
Output [1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5 Article Tags : Java Java - util package Java-Collections java-list Practice Tags : Java Java-Collections |