.hasPrevious()
Published Jun 30, 2022
Contribute to Docs
The .hasPevious()
method returns true
if a ListIterator
object has prior elements (i.e., elements before the current element). Returns false
otherwise.
Syntax
Boolean value = iterator.hasPevious();
Where iterator
is a ListIterator
object.
Example
The following example uses a ListIterator
to traverse an ArrayList
and then traverse back:
import java.util.*;public class Example {public static void main(String args[]) {// Create a new ArrayListArrayList l = new ArrayList();// Add some items to the ArrayListl.add(1);l.add(2);l.add(3);l.add(4);l.add(5);ListIterator i = l.listIterator();// Loop through ArrayList contentswhile(i.hasNext()) {Object item = i.next();System.out.print(item + ", ");}System.out.println();// Loop back through ArrayList contentswhile(i.hasPrevious()) {Object item = i.previous();System.out.print(item + ", ");}}}
This example outputs the following:
1, 2, 3, 4, 5,5, 4, 3, 2, 1,
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.