This cycle will never end. it.hasNext does not promote an iterator. You must call it .next () to promote it. The loop probably ends because k becomes 5, at which point the Arraylist discards the exception of boundaries.
The correct form for iterating a list (containing strings):
Iterator it = ac.iterator();
while (it.hasNext) {
System.out.println((String) it.next());
}
Or if the list is entered, for example. Arraylist
for (String s : ac) {
System.out.println((String) s);
}
Or, if you absolutely know that this is a list of arrays and you need speed compared:
for (int i = 0; i < ac.size(); i++) {
System.out.println(ac.get(i));
}
locka source
share