Java Program to Add an Element to ArrayList using ListIterator Last Updated : 15 Dec, 2020 Comments Improve Suggest changes Like Article Like Report 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] Comment More infoAdvertise with us Next Article Java Program to Add an Element to ArrayList using ListIterator K KapilChhipa Follow Improve Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java 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 How to Add an Element at Particular Index in Java ArrayList? ArrayList.add() method is used to add an element at particular index in Java ArrayList. Syntax: public void add(int index, Object element) ; Parameters: index -position at which the element has to be inserted. The index is zero-based.element - the element to be inserted at the specified position. Ex 2 min read Get Previous and Next Index using Java ListIterator The previous index and next index in an ArrayList can be obtained using the methods previousIndex() and nextIndex() respectively of the ListIterator interface. previousIndex() can also return -1 if it at the beginning of the list. Example: Input: list = [2, 3, 6, 8] listiterator is at the beginning 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 How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read Like