Java Program to Add an Element to ArrayList using ListIterator
Last Updated :
15 Dec, 2020
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 the given index.
Syntax:
// listIterator() method returns an iterator over the list from the beginning
ListIterator<Integer> it = list.listIterator()
// listIterator(index) method returns ans iterator over the list from the given index
ListIterator<Integer> it = list.listIterator(index);
Return Value: This method returns a list iterator over the elements in this list (in proper sequence).
Approach: To add an element to ArrayList using Java ListIterator, the process divided into two parts:
1. Make an iterator.
ListIterator<Integer> it = list.listIterator()
2. Add element using add() method.
it.add(element)
Below is the implementation of the above approach:
Example 1:
Java
// Java program to add elements to ArrayList using
// ListIterator
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// New empty ArrayList
List<Integer> list = new ArrayList<>();
// Add elements to ArrayList
list.add(10);
list.add(20);
list.add(30);
// Print ArrayList before add 50
System.out.println("Before add 50: " + list);
// ListIterator
ListIterator<Integer> it = list.listIterator();
// iterate ArrayList and add 50 to ArrayList
while (it.hasNext()) {
// add 50 to ArrayList
it.add(50);
// move iterator
it.next();
}
// Print ArrayList after add 50
System.out.println("After add 50: " + list);
}
}
OutputBefore add 50: [10, 20, 30]
After add 50: [50, 10, 50, 20, 50, 30]
Example 2:
Java
// Java program to add elements to ArrayList using
// ListIterator
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// New empty ArrayList
List<Integer> list = new ArrayList<>();
// Add elements to ArrayList
list.add(10);
list.add(20);
list.add(30);
// Print ArrayList before add 50
System.out.println("Before add 50: " + list);
// Returns an iterator over the list from the given
// index
ListIterator<Integer> it = list.listIterator(1);
// iterate ArrayList and add 50 to ArrayList
while (it.hasNext()) {
// add 50 to ArrayList
it.add(50);
// move iterator
it.next();
}
// Print ArrayList after add 50
System.out.println("After add 50: " + list);
}
}
OutputBefore add 50: [10, 20, 30]
After add 50: [10, 50, 20, 50, 30]
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
Replace an Element From ArrayList using Java ListIterator 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 bel
3 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 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 Copy and Add all List Elements to an Empty ArrayList in Java? We can copy and add List items in Array List using addAll() method. This method accepts a Collection (Ex. List) as an argument and adds the collection items at the end of its calling Collection (Ex. ArrayList). This method returns a boolean value. addAll() return true if the collection successfully
2 min read
Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is
3 min read