Replace an Element From ArrayList using Java ListIterator
Last Updated :
03 Apr, 2023
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 element
- Replacing Last element
Replacing First element:
Approach
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
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..." );
for ( int i : list) {
System.out.println(i);
}
ListIterator<Integer> iterator = list.listIterator();
iterator.next();
iterator.set( 10 );
System.out.println( "After replacing..." );
for ( int i : list) {
System.out.println(i);
}
}
}
|
Output
Before Replacing...
9
11
12
13
14
15
After replacing...
10
11
12
13
14
15
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
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
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..." );
while (iterator.hasNext()) {
System.out.print(iterator.next()+ " " );
}
iterator.set( "Mohit" );
System.out.println();
System.out.println( "After Replacing..." );
for (String n : name) {
System.out.print(n+ " " );
}
}
}
|
Output
Before Replacing...
Yash Akash Amar Abhishek Rajnikanth
After Replacing...
Yash Akash Amar Abhishek Mohit
Time Complexity: O(n)
Auxiliary Space: O(1)
As constant extra space is used.
Share your thoughts in the comments
Please Login to comment...