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
// 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);
}
}
}
OutputBefore 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
// 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+" ");
}
}
}
OutputBefore 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.
Similar Reads
Java Program to Remove an Element from ArrayList using ListIterator ListIterator.remove() method removes the last element from the list that was returned by next() or previous() cursor positions. It can be called only once per call to next or previous. It can be made only if the operation â add(E) has not called after the last call to next or previous. Internal work
4 min read
Java Program to Add an Element to ArrayList using ListIterator In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from
2 min read
Removing last element from ArrayList in Java Given an ArrayList collection in Java, the task is to remove the last element from the ArrayList. Example: Input: ArrayList[] = [10, 20, 30, 1, 2] Output: [10, 20, 30, 1] After removing the last element 2, the ArrayList is: [10, 20, 30, 1] Input: ArrayList[] = [1, 1, 2, 2, 3] Output: [1, 1, 2, 2] Af
2 min read
How to Replace a Element in Java ArrayList? To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
2 min read
Java Program to Replace an Element in a List List in Java provides a way to store the ordered collection. The list Interface provides various methods to add, delete, or replace items in the List. In this article, we will learn about how we could replace an element in the list in Java. Methods to Replace an Element in a ListThere are 3 ways to
4 min read
How to Swap Two Elements in an ArrayList in Java? We can swap two elements of Array List using Collections.swap() method. This method accepts three arguments. The first argument is the ArrayList and the other two arguments are the indices of the elements. This method returns nothing. Syntax: public static void swap(List list, int a, int b); Parame
2 min read