回答編集履歴

4

修正

2020/10/31 09:15

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -66,13 +66,9 @@
66
66
 
67
67
  class MyList extends ArrayList<Integer> {
68
68
 
69
- private List<Integer> list;
69
+ MyList(List<Integer> arg) {
70
70
 
71
-
72
-
73
- MyList(List<Integer> list) {
74
-
75
- this.list = list;
71
+ super(arg);
76
72
 
77
73
  }
78
74
 
@@ -84,7 +80,7 @@
84
80
 
85
81
  System.out.println("イテレータが作られてるよ!");
86
82
 
87
- return list.iterator();
83
+ return super.iterator();
88
84
 
89
85
  }
90
86
 
@@ -114,7 +110,7 @@
114
110
 
115
111
 
116
112
 
117
- **実行結果** [Wandbox](https://wandbox.org/permlink/uWq7lk46efECmPxj)
113
+ **実行結果** [Wandbox](https://wandbox.org/permlink/XKnRHkyIh2obScN4)
118
114
 
119
115
  ```
120
116
 

3

追記

2020/10/31 09:15

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -47,3 +47,115 @@
47
47
  直接的に役立つことはまず無いでしょう。
48
48
 
49
49
  ただし、仕組みを知ることは大切です。これは何にでも言えることですが。
50
+
51
+
52
+
53
+ 実験
54
+
55
+ ---
56
+
57
+ MyList#iteratorを明示的に呼び出していないのがミソです。
58
+
59
+
60
+
61
+ ```Java
62
+
63
+ import java.util.*;
64
+
65
+
66
+
67
+ class MyList extends ArrayList<Integer> {
68
+
69
+ private List<Integer> list;
70
+
71
+
72
+
73
+ MyList(List<Integer> list) {
74
+
75
+ this.list = list;
76
+
77
+ }
78
+
79
+
80
+
81
+ @Override
82
+
83
+ public Iterator<Integer> iterator() {
84
+
85
+ System.out.println("イテレータが作られてるよ!");
86
+
87
+ return list.iterator();
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+ class Main {
98
+
99
+ public static void main(String[] args) {
100
+
101
+ List<Integer> list = new MyList(List.of(3, 1, 4));
102
+
103
+ for(int e: list) {
104
+
105
+ System.out.println(e);
106
+
107
+ }
108
+
109
+ }
110
+
111
+ }
112
+
113
+ ```
114
+
115
+
116
+
117
+ **実行結果** [Wandbox](https://wandbox.org/permlink/uWq7lk46efECmPxj)
118
+
119
+ ```
120
+
121
+ イテレータが作られてるよ!
122
+
123
+ 3
124
+
125
+ 1
126
+
127
+ 4
128
+
129
+ ```
130
+
131
+
132
+
133
+ 参考
134
+
135
+ ---
136
+
137
+ > The enhanced for statement is equivalent to a basic for statement of the form:
138
+
139
+
140
+
141
+ > ```Java
142
+
143
+ for (I #i = Expression.iterator(); #i.hasNext(); ) {
144
+
145
+ {VariableModifier} TargetType Identifier =
146
+
147
+ (TargetType) #i.next();
148
+
149
+ Statement
150
+
151
+ }
152
+
153
+ > ```
154
+
155
+
156
+
157
+ > `#i` is an automatically generated identifier that is distinct from any other identifiers (automatically generated or otherwise) that are in scope (§6.3) at the point where the enhanced for statement occurs.
158
+
159
+
160
+
161
+ **引用元**:[Java Language Specification - The enhanced for statement](https://docs.oracle.com/javase/specs/jls/se15/html/jls-14.html#jls-14.14.2)

2

追記

2020/10/31 09:13

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -34,4 +34,16 @@
34
34
 
35
35
  これ、まさにIteratorパターンですよね?
36
36
 
37
- いちいちイテレータを呼んでいては面倒なので、内部で処理されているだけです。
37
+ 実装者がいちいちイテレータを呼んでいては面倒なので、内部で処理されているだけです。
38
+
39
+
40
+
41
+ > Iteratorパターンを学んだことで、ArrayListのiteratorの動き自体への理解は深まりましたが、
42
+
43
+ 現実のプロジェクトにどう当てはめれば良いのだろうと、疑問が残りました。
44
+
45
+
46
+
47
+ 直接的に役立つことはまず無いでしょう。
48
+
49
+ ただし、仕組みを知ることは大切です。これは何にでも言えることですが。

1

追記

2020/10/31 09:05

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -1,3 +1,7 @@
1
+ List<Integer>型の変数listがあるとします。
2
+
3
+
4
+
1
5
  ```Java
2
6
 
3
7
  for(int e: list) {