I want to be able to insert elements to the ArrayList<String>
using ListIterator
, but somehow I am confused even after reading the documentation related to the add method of the ListIterator
class, if I do something like this
for(int i = 0 ; i < list.size() ; ++i)
listIterator.add( list.get(i) );
What does this code snippet do to my list iterator, where does it move the list iterator?
When I run the following code I get the result as "Hi" -:
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIter {
public static void main(String[] args) {
String[] s = {"Hi", "I", "am", "Ankit"};
ArrayList<String> list = new ArrayList<>();
ListIterator<String> listIterator = list.listIterator();
for (int i = 0; i < s.length; ++i) {
listIterator.add(s[i]);
}
while (listIterator.hasPrevious()) {
listIterator.previous();
}
System.out.println(listIterator.next());
}
}
Kindly tell how is this output being generated?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…