Java study note 28 (set framework 2: Iterator iterator), study note Iterator

Source: Internet
Author: User

Java study note 28 (set framework 2: Iterator iterator), study note Iterator

Iterator interface, translated into Chinese as an Iterator

The concept of iterator is interpreted as follows:

There are many collections in java. There are various internal storage methods and different retrieval methods. Is there a common way to obtain them?

This method is similar to for loop traversal,

General method of getting an element: Determine whether this element exists in the set before retrieving the element. If yes, retrieve the element. Continue to judge whether the element exists. If yes, continue to retrieve the element until all is retrieved. This kind of retrieval is iterative.

 

Implementation of the iterator:

(You can also change the ArrayList set to another set)

Package demo; import java. util. arrayList; import java. util. collection; import java. util. iterator; public class IteratorDemo {public static void main (String [] args) {Collection <String> c1 = new ArrayList <String> (); c1.add ("abc1 "); c1.add ("abc2"); c1.add ("abc3"); c1.add ("abc4"); Iterator <String> it1 = c1.iterator (); boolean b1 = it1.hasNext (); // whether there are elements that can be taken out of System. out. println (b1); // true // obtain the element while (it1.hasNext () {String s = it1.next (); System. out. println (s );}}}

 

Transformation in collection iteration:
We recommend that you add a type, but do not add it. You can also execute:

Package demo; import java. util. arrayList; import java. util. collection; import java. util. iterator; public class CollectionDemo {public static void main (String [] args) {// set can store any type of objects // set, without specifying the data type, you can also store Collection c1 = new ArrayList (); c1.add ("abc"); c1.add ("def"); Iterator it1 = c1.iterator (); while (it1.hasNext ()) {// it. next gets the Object type and forcibly converts String s1 = (String) it1.next (); System. out. println (s1.length ());}}}

 

Using the principle of the iterator, an enhanced for loop occurs after JDK1.5:

Example of enhanced for loop traversal array:

Package demo; // enhance the for loop with a fixed format. // advantages: less code, convenient traversal // disadvantages: No index, the public class ForEachDemo {public static void main (String [] args) {function1 (); function2 ();} public static void function1 () in the container cannot be operated () {int [] arr = {3, 2, 5, 4, 8, 9, 6}; // traverses the output for (int I: arr) {System. out. println (I) ;}} public static void function2 () {// when traversing, you can call the method String [] str1 = {"abc", "def ", "java"}; for (String s: str1) {System. out. println (s); System. out. println (s. length ());}}}

 

Example of an enhanced for loop traversal set:

Package demo; import java. util. arrayList; // enhances the for loop. The regular format is public class ForEachDemo {public static void main (String [] args) {function ();} public static void function () {ArrayList <String> array1 = new ArrayList <String> (); array1.add ("abcd"); array1.add ("java"); for (String s: array1) {System. out. println (s );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.