This API:
/// Returns a list iterator of the elements in this list (in proper sequence)
public ListIterator<E> listIterator(int index)
What is the meaning of proper sequence?
List<Integer> list = new Arr开发者_StackOverflow中文版ayList<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(); // Is result 2? Or random?
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;
public class Main
{
public static void main(String[] args)
{
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());
}
}
}
The output is:
1
2
3
1
2
3
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".
Yes the result will be 2, proper sequence for array lists means in order of index.
精彩评论