Replace an Element From ArrayList using Java ListIterator - GeeksforGeeks

07 Jan, 2021
To replace an element from an ArrayList, the set() method of ListIterator interface can be used. set() method of ListIterator replaces the last element which is returned by the next() or previous() methods, along with the given element.
Two ways of replacing the elements using ListIterator shown below are:
Replacing First elementReplacing Last element
Replacing First element
Replacing Last element
Replacing First element:
Approach
Creating an ArrayList
ArrayList<Integer> list = new ArrayList<Integer>();

list.add(9);
list.add(11);
list.add(12);
list.add(13);
list.add(14);
list.add(15):
Suppose we need to replace the first element of the list which is 9 with 10. First, we will use ListIterator and return the next element in the List with next() method.
ListIterator<Integer> iterator = list.listIterator();

iterator.next();
Now, using set() method replace the element in the List. Here we will be replacing first element of the iterator which can be replaced with any specified value ( in our case we need to replace it with 10 ).
iterator.set(10);
Java
// Java program to replace an element// from ArrayList using Java ListIterator  import java.util.ArrayList;import java.util.ListIterator;  class GFG {    public static void main(String[] args)    {        ArrayList<Integer> list = new ArrayList<Integer>();        list.add(9);        list.add(11);        list.add(12);        list.add(13);        list.add(14);        list.add(15);          System.out.println("Before Replacing...");                  // Printing the original list        for (int i : list) {                        System.out.println(i);                    }                ListIterator<Integer> iterator = list.listIterator();                // Replacing the first element        iterator.next();        iterator.set(10);                System.out.println("After replacing...");                // Printing the list after replacement        for (int i : list) {                        System.out.println(i);                    }    }}
Before Replacing...
9
11
12
13
14
15
After replacing...
10
11
12
13
14
15
Replacing the last element:
Approach
Using the same above method, any element of the ArrayList at any position, can be replaced with any value.
Creating an ArrayList of names and add a few names to it –
ArrayList<String> name = new ArrayList<>();

name.add("Yash");
name.add("Akash");
name.add("Amar");
name.add("Abhishek");
name.add("Rajnikanth");
Let us assume that we need to replace “Rajnikanth” with “Mohit“.
Now, using a ListIterator, we will iterate to the last element of the list and then replace it –
ListIterator<String> iterator = name.listIterator();
while(iterator.hasNext())
{
    iterator.next();    
}

iterator.set("Mohit");
Java
// java program to replace an element// from ArrayList using Java ListIterator  import java.util.ArrayList;import java.util.ListIterator;  class GFG {    public static void main(String[] args)    {        ArrayList<String> name = new ArrayList<>();        name.add("Yash");        name.add("Akash");        name.add("Amar");        name.add("Abhishek");        name.add("Rajnikanth");          ListIterator<String> iterator = name.listIterator();          System.out.println("Before Replacing...");                  // Printing the original list        while (iterator.hasNext()) {            System.out.print(iterator.next()+" ");        }                   // Simce the iterator was on last element        // so the set function used here will replace        // the last element        iterator.set("Mohit");                  System.out.println();        System.out.println("After Replacing...");          for (String n : name) {            System.out.print(n+" ");        }    }}
Before Replacing...
Yash Akash Amar Abhishek Rajnikanth 
After Replacing...
Yash Akash Amar Abhishek Mohit
Java-ArrayList
Java-Collections
Java-Iterator
Picked
Java
Java Programs
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character