List can be traversed by ordinal, but it is usually recommended to use iterator to traverse
Iterator ITR = List.iterator ();
while (Itr.hasnext ()) {
Object nextobj = Itr.next ();
}
Using the Clear () method is easiest if you want to delete all of them.
In addition, the iterator has a remove () method, which can be removed at the time of traversal, depending on certain conditions.
Example:
Import java.util.*;
public class Test {
public static void print (List<integer> List) {
iterator<integer> ITR = List.iterator ();
while (Itr.hasnext ()) {
System.out.print (Itr.next ());
System.out.print (",");
}
System.out.println ();
}
public static void Main (string[] args) {
list<integer> s = new arraylist<integer> ();
for (Integer i = 0; i < i++) {
S.add (i);
}
print (s);
iterator<integer> ITR = S.iterator ();
while (Itr.hasnext ()) {
Integer i = Itr.next ();
if (i% 3 = = 0) {
Itr.remove ();
}
}
print (s);
}
}
Methods for Java list traversal