Today, talk about the use of set iterator iterators and set subscripts
First on the code
Import Java.util.arraylist;import Java.util.iterator;public class Test {public Test () {//Initialize listadditemtolist ();} Private arraylist<string> list = new arraylist<string> ();p rivate void Additemtolist () {for (int i = 0; i < 5; i++) {List.add (i + "");}} Public arraylist<string> getList () {return list;} public void setlist (arraylist<string> list) {this.list = list;} public static void Main (string[] args) {Test test1 = new Test (); arraylist<string> test1list = Test1.getlist (); for (int i = 0; i < test1list.size (); i++) {//use subscript here, point directly to Element Test1li St.remove (i);} System.out.println ("Test1list has been removed all elements"); Test test2 = new test (); arraylist<string> test21list = Test2.getlist (); for (iterator<string> Iterator = Test21list.iterator (); Iterator.hasnext ();) {//Use iterators to set operations, you need to do next ()//Next () similar to a cursor, he is between two elements///Unlike the above directly pointing to an element Iterator.next (); Iterator.remove ();} System.out.println ("Test2list has been removed all elements");}}
Note the point:
1. when using iterators for collection operations, you need to do next ()
2.Next () is similar to a cursor, which is between two elements, and the subscript directly points to the element (see details)
Getting Started with Java Basics-iterator iterators and the use of set subscripts