How do I retrieve LinkedList objects using ListIterator?

Category: java.util, viewed: 92 time(s).

This example show you how to use the ListIterator interface to retrieves objects from a LinkedList.

package org.kodejava.example.util;

import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<String>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        //
        // Retrieves object from LinkedList using the ListIterator
        // interface.
        //
        ListIterator li = grades.listIterator();
        while (li.hasNext()) {
            System.out.println("Grades: " + li.next());
            System.out.println("    hasPrevious  : " + li.hasPrevious());
            System.out.println("    hasNext      : " + li.hasNext());
            System.out.println("    previousIndex: " + li.previousIndex());
            System.out.println("    nextIndex    : " + li.nextIndex());
        }
    }
}

The program will prints the following output:

Grades: A
    hasPrevious  : true
    hasNext      : true
    previousIndex: 0
    nextIndex    : 1
Grades: B
    hasPrevious  : true
    hasNext      : true
    previousIndex: 1
    nextIndex    : 2
Grades: C
    hasPrevious  : true
    hasNext      : true
    previousIndex: 2
    nextIndex    : 3
Grades: D
    hasPrevious  : true
    hasNext      : true
    previousIndex: 3
    nextIndex    : 4
Grades: E
    hasPrevious  : true
    hasNext      : false
    previousIndex: 4
    nextIndex    : 5
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Can't find what you are looking for? Join our FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats