tags:

views:

69

answers:

3

In the API:

public ListIterator<E> listIterator(int index)

Returns a list iterator of the elements in this list (in proper sequence)

What is the meaning of proper sequence?

List<Integer> list=new ArrayList<Integer>();
list.add(1);list.add(2);list.add(3);

/**IS the sequence returned by i1 and i2 is the same?*/    
ListIterator<Integer> i1=list.listIterator();
ListIterator<Integer> i2=list.listIterator();


i1.next();

int result=i1.next();//Does result must be 2?or randomly?
+2  A: 

Yes the result will be 2, proper sequence for array lists means in order of index.

rsp
+4  A: 

when iterate in ListIterator object return in order of index proper mean that, and i1 , i2 is independent :

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * @author sjb
 */
public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here

        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);

        /**IS the sequence returned by i1 and i2 is the same?*/
        ListIterator<Integer> i1 = list.listIterator();
        ListIterator<Integer> i2 = list.listIterator();

        while (i1.hasNext())
        {
            System.out.println(i1.next());
        }
        while (i2.hasNext())
        {
            System.out.println(i2.next());
        }    
    }
}

output is :

1
2
3
1
2
3
SjB
+2  A: 

Returns a list iterator of the elements in this list (in proper sequence)

This means the ListIterator.next() method will return the list's elements in the order that they appear in the list. The same applies in the other places in the List javadocs where they use the phrase "the proper sequence".

Stephen C