-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTIterableStudent.java
More file actions
90 lines (70 loc) · 1.65 KB
/
Copy pathTIterableStudent.java
File metadata and controls
90 lines (70 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package collection;
import java.util.Iterator;
/**
* @author:chenxun
* createDate:2016年11月21日 下午11:30:04
* Theme:
* reference:
* descript:
*/
public class TIterableStudent {
public static void main(String[] args) {
StudentIterable studentS = new StudentIterable(10);
for (Student student : studentS) {
System.out.println(student.toString());
}
}
private static class StudentIterable implements Iterable<Student>{
private Student[] students;
public StudentIterable(int size) {
this.students = new Student[size];
for (int i = 0; i < size; i++) {
students[i] = new Student("student_"+(i+1), 20+i);
}
}
@Override
public Iterator<Student> iterator() {
return new StudentIterator();
}
private class StudentIterator implements Iterator<Student>{
private int index = 0;
@Override
public boolean hasNext() {
return index!=students.length;
}
@Override
public Student next() {
return students[index++];
}
}
}
private static class Student {
private String name;
private Integer age;
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
@SuppressWarnings("unused")
public String getName() {
return name;
}
@SuppressWarnings("unused")
public void setName(String name) {
this.name = name;
}
@SuppressWarnings("unused")
public Integer getAge() {
return age;
}
@SuppressWarnings("unused")
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
}