Java-9.13 foreach and iterator
In this chapter, let's take a look at foreach and Iterator.
1. Example:
package com.ray.ch09;import java.util.ArrayList;public class Test {public static void showItems(Iterable
iterator) {for (Integer item : iterator) {System.out.println(item);}}public static void main(String[] args) {ArrayList
list = new ArrayList
();for (int i = 0; i < 10; i++) {list.add(i);}showItems(list);}}
Are the examples above very familiar, but they are also different?
Because we have used foreach, but added the Iterable interface.
In foreach implementation, foreach can be used as long as containers that implement the Iterable interface are used. Therefore, although foreach is generally used in arrays, it can also be used in most containers.
Let's modify the code in the previous chapter to form a new code:
Package com. ray. ch09; import java. util. Iterator; public class Test {public static void showItems (Iterator
Iterator) {while (iterator. hasNext () {Person person = iterator. next (); System. out. print (person. getId () +) ;}} public static void showItems (Iterable
Iterator) {for (Person person: iterator) {System. out. println (person. getId () ;}} public static void main (String [] args) {PersonList personList = new PersonList (); showItems (personList. iterator (); showItems (personList) ;}} class Person {private int id = 0; public int getId () {return id;} public void setId (int id) {this. id = id ;}} class PersonList implements Iterable
{Private Person [] persons = new Person [10]; public PersonList () {for (int I = 0; I <persons. length; I ++) {Person person Person = new person (); Person. setId (I); persons [I] = person;} public Iterator
Iterator () {return new Iterator
() {Private int index = 0; @ Overridepublic boolean hasNext () {return index <persons. length ;}@ Overridepublic Person next () {return persons [index ++] ;}@ Overridepublic void remove () {// future implementation }};}}
The code is basically the same, except that Iterable is no longer used during traversal, but foreach is only used.
Note: The array and the class implementing the Iterable interface are put together. But if you implement the Iterable interface for the array, the problem arises. See the following code.
package com.ray.ch09;import java.util.Arrays;public class Test {public static void showItems(Iterable
iterator) {for (Integer item : iterator) {System.out.println(item);}}public static void main(String[] args) {Integer[] array = new Integer[10];for (int i = 0; i < 10; i++) {array[i] = i;}// showItems(array);//errorshowItems(Arrays.asList(array));}}
If the number is passed as a parameter, an exception is thrown. It can only be used after the array is converted to a list.
Of course, if the parameters we accept are arrays, they will be different.
package com.ray.ch09;public class Test {public static void showItems(Integer[] items) {for (Integer item : items) {System.out.println(item);}}public static void main(String[] args) {Integer[] array = new Integer[10];for (int i = 0; i < 10; i++) {array[i] = i;}showItems(array);}}
Summary: This chapter describes the relationship between foreach and Iterable.