Avatar of royjayd
royjayd
 asked on

ListIterator v/s Iterator

hi experts
Can anyone explain Iterator v/s Listiterator with a very simple ArrayList example.
thanks.
AlgorithmsJava

Avatar of undefined
Last Comment
royjayd

8/22/2022 - Mon
kaufmed

What programming language is this for?
royjayd

ASKER
Java
TommySzalapski

I sent a note to the admins to add this to the Java zone.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
mccarl

Have you looked at the Javadocs for ListIterator? I don't know whether I could really say more without just repeating what is already documented.

Have a read of that link and then if you still have more specific questions, post back here!
royjayd

ASKER
Well I read the docs,  I am looking for a simple example of iterating an arraylist with list iterator. I have used  iterator in the past. Just want to understand a practical usage of list iterator.
mrcoffee365

I think  mccarl  above answered your question.  However, you can always check the java2s site for examples of java code, like this:

   ArrayList<String> al = new ArrayList<String>();
// add some data to al here then iterate
    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
      String element = itr.next();
      System.out.print(element + " ");
    }
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
royjayd

ASKER
The focus is on ListIterator here not Iterator.
When would I use a ListIterator instead of
iterator?
mrcoffee365

Ah -- it's beginning to sound like homework.  

But you can look at various code examples, if that helps:
http://www.java2s.com/Code/JavaAPI/java.util/ArrayListiterator.htm

You should check the Java API as mccarl suggested and try them or ask a specific question.  I don't think there's much difference between the two, except that of course ListIterator is meant to work on List collections and an Iterator can work on other types of collections.
ASKER CERTIFIED SOLUTION
mccarl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
royjayd

ASKER
thanks for the example. I also see they use ListIterator heavily in Java api, for instance
Collections.sort(list);

    public static <T extends Comparable<? super T>> void sort(List<T> list) {
        Object[] a = list.toArray();
        Arrays.sort(a);
        ListIterator<T> i = list.listIterator();
        for (int j=0; j<a.length; j++) {
            i.next();
            i.set((T)a[j]);
        }
    }

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
mccarl

Yeah, so the "set()" method is the other main addition that ListIterator has over Iterator. Thinking a bit more about it, that is probably the method that may have the most real world use.
royjayd

ASKER
true, there is also add(e) which is intresting.