Import Java. util. collection; Import Java. util. hashset; Import Java. util. iterator; Public Class Testiterator { Public Static Void Main (string [] ARGs) {collection books = New Hashset (); books. Add ( "Java handouts" ); Books. Add ( "Java Ajax" ); Books. Add ( "Java authoritative guide" ); // Get the iterator corresponding to the books set Iterator it = Books. iterator (); While (It. hasnext ()){ // The data type returned by it. hasnext () is object, so forced type conversion is required. String book = (String) it. Next (); system. Out. println (book ); If (Books. Equals ("Java handout" ) {It. Remove ();} // Copying a book does not change the book itself. Book = "test string" ;} System. Out. println (books );}}
Import Java. util. collection; Import Java. util. hashset; Import Java. util. iterator; Public Class Testiterator { Public Static Void Main (string [] ARGs) {collection books = New Hashset (); books. Add ( "Java handouts" ); Books. Add ( "Java Ajax" ); Books. Add ( "Java authoritative guide" ); // Get the iterator corresponding to the books set Iterator it = Books. iterator (); While (It. hasnext ()){ // The data type returned by it. hasnext () is object, so forced type conversion is required. String book = (String) it. Next (); system. Out. println (book ); If (Book. Equals ("Java handout" )){ // The Set elements cannot be modified during iterator iteration.CodeException thrown Books. Remove (book );}} // System. Out. println (books ); }}
Java Ajax
Java Handouts
Java authoritative guide
[Java Ajax, Java handouts, Java authoritative guide]