Get Previous and Next Index using Java ListIterator Last Updated : 03 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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 find previous index. Output: -1 Input: list = [2, 3, 6, 8] listiterator is at the beginning find next index. Steps to use previousIndex() and nextIndex(): Create an empty ArrayListAdd elements to ArrayList.Create a listiterator using listIterator() method.Listiterator<Integer>iterator = arrList.listIterator();Now get the required index using the below commands Syntax iterator.previousIndex(); Returns: the index of the element that would be returned by a subsequent call to previous, or -1 if the list iterator is at the beginning of the list Syntax iterator.nextIndex(); Returns: the index of the element that would be returned by a subsequent call to next, or list size if the list iterator is at the end of the list Example Java // Java program to get Previous and // next index using ListIterator import java.io.*; import java.util.ArrayList; import java.util.ListIterator; class PreviousAndNextIndex { public static void main(String[] args) { // create empty ArrayList ArrayList<Integer> arrList = new ArrayList<Integer>(); // add elements to the ArrayList arrList.add(5); arrList.add(10); arrList.add(15); arrList.add(20); arrList.add(25); arrList.add(30); // print the initial list System.out.println("Initial arraylist =>" + arrList); // initializing ListIterator ListIterator<Integer> iterator = arrList.listIterator(); // initially iterator is the beginning so // previousIndex() will return -1 System.out.println("previous index =>" + iterator.previousIndex()); // from -1 moving iterator to the 1st index iterator.next(); iterator.next(); // now iterator is at 1st index // so nextIterator() will return 2 System.out.println("Next index =>" + iterator.nextIndex()); } } Output: Initial arraylist =>[5, 10, 15, 20, 25, 30] previous index =>-1 Next index =>2 Time Complexity: O(n) Auxiliary Space: O(1) As constant extra space is used. Comment More infoAdvertise with us Next Article Get Previous and Next Index using Java ListIterator patelnagendra1 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads 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 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 Difference between an Iterator and ListIterator in Java Iterators are used in Collection framework in Java to retrieve elements one by one. It can be applied to any Collection object. By using Iterator, we can perform both read and remove operations. Iterator must be used whenever we want to enumerate elements in all Collection framework implemented inte 3 min read Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde 4 min read Java Program For Writing A Function To Get Nth Node In A Linked List Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before moving on to th 4 min read Java Program to find the Last Index of a Particular Word in a String To find out the last occurrence of a character or string from a given string across which the substring or character is been searched over and returning the index value of the character or substring is found. Real-life Example: Consider an example of a book. The book contains pages where pages conta 4 min read Searching Elements in Vector Using Index in Java Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then th 4 min read Iterate a LinkedList in Reverse Order in Java For traversing a linked list in reverse order we can use Descending Iterator or List Iterator 1. Descending Iterator Syntax: LinkedList<String> linkedlist = new LinkedList<>(); Iterator<String> listIterator = linkedlist.descendingIterator(); Returns: Descending Iterator returns the 2 min read How to Get First and Last Element From the Vector in Java? The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any par 2 min read How to Find a Sublist in a List in Java? List in Java contains index-based methods. This enables us to maintain the order collection. So this enables us to search, insert, delete, and even update the elements. This enables us to store multiple copies of the same element already existing on our list. Also, in addition, null elements are all 5 min read Like