回答編集履歴

4

Portfolio クラスの Iterable 化

2021/12/09 15:21

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -10,19 +10,19 @@
10
10
 
11
11
  専用メソッドも課題的に問題視されるかもしれませんので、元の形にしました。
12
12
 
13
+ Portfolio を for-each ループで使えるように Iterable 化しました。
14
+
13
15
  ```java
14
16
 
15
17
  package teratail_java.q373056;
16
18
 
17
19
 
18
20
 
19
- import java.util.Objects;
21
+ import java.util.*;
20
-
21
- import java.util.StringJoiner;
22
+
22
-
23
-
24
-
23
+
24
+
25
- public class Portfolio {
25
+ public class Portfolio implements Iterable<Position> {
26
26
 
27
27
  private Position[] positions = new Position[0];
28
28
 
@@ -58,7 +58,7 @@
58
58
 
59
59
  private Position findPosition(Issue issue) {
60
60
 
61
- for(int i=0; i<length; i++) if(positions[i].getIssue().equals(issue)) return positions[i];
61
+ for(Position p : this) if(p.getIssue().equals(issue)) return p;
62
62
 
63
63
  return null;
64
64
 
@@ -74,6 +74,34 @@
74
74
 
75
75
  }
76
76
 
77
+ @Override
78
+
79
+ public Iterator<Position> iterator() {
80
+
81
+ return new Iterator<Position>() {
82
+
83
+ private int index = 0;
84
+
85
+ @Override
86
+
87
+ public Position next() {
88
+
89
+ return positions[index++];
90
+
91
+ }
92
+
93
+ @Override
94
+
95
+ public boolean hasNext() {
96
+
97
+ return index < length;
98
+
99
+ }
100
+
101
+ };
102
+
103
+ }
104
+
77
105
 
78
106
 
79
107
  public static void main(String[] args) {
@@ -108,9 +136,9 @@
108
136
 
109
137
 
110
138
 
111
- for(int i=0; i<portfolio.getLength(); i++) {
139
+ for(Position p : portfolio) {
112
-
140
+
113
- System.out.println(portfolio.getPosition(i));
141
+ System.out.println(p);
114
142
 
115
143
  }
116
144
 

3

修正の説明の追加

2021/12/09 15:21

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  そのままというのも勿体ない(?)ので、段階的拡張としてみました。
10
10
 
11
+ 専用メソッドも課題的に問題視されるかもしれませんので、元の形にしました。
12
+
11
13
  ```java
12
14
 
13
15
  package teratail_java.q373056;
@@ -74,8 +76,6 @@
74
76
 
75
77
 
76
78
 
77
-
78
-
79
79
  public static void main(String[] args) {
80
80
 
81
81
  Position positionStock1 = new Position(new Stock("92010", "A", Stock.Market.TSE), 0.1);

2

main での for で表示するようにコード変更

2021/12/09 13:10

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -62,11 +62,17 @@
62
62
 
63
63
  }
64
64
 
65
+ public int getLength() { return length; }
66
+
65
- void printAll() {
67
+ public Position getPosition(int index) {
66
-
68
+
67
- for(int i=0; i<length; i++) System.out.println(positions[i]);
69
+ if(index < 0 || length <= index) throw new ArrayIndexOutOfBoundsException(index);
70
+
68
-
71
+ return positions[index];
72
+
69
- }
73
+ }
74
+
75
+
70
76
 
71
77
 
72
78
 
@@ -102,7 +108,11 @@
102
108
 
103
109
 
104
110
 
111
+ for(int i=0; i<portfolio.getLength(); i++) {
112
+
105
- portfolio.printAll();
113
+ System.out.println(portfolio.getPosition(i));
114
+
115
+ }
106
116
 
107
117
  }
108
118
 

1

配列仕様化

2021/12/09 13:06

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -2,19 +2,29 @@
2
2
 
3
3
 
4
4
 
5
+ ----
6
+
7
+ コレクション不可ということですので配列に戻しました。
8
+
9
+ そのままというのも勿体ない(?)ので、段階的拡張としてみました。
10
+
5
11
  ```java
6
12
 
7
13
  package teratail_java.q373056;
8
14
 
9
15
 
10
16
 
11
- import java.util.*;
17
+ import java.util.Objects;
18
+
19
+ import java.util.StringJoiner;
12
20
 
13
21
 
14
22
 
15
23
  public class Portfolio {
16
24
 
17
- private List<Position> positionList = new ArrayList<>();
25
+ private Position[] positions = new Position[0];
26
+
27
+ private int length = 0;
18
28
 
19
29
  public void addPosition(Position p) {
20
30
 
@@ -22,7 +32,9 @@
22
32
 
23
33
  if(exists == null) {
24
34
 
35
+ if(length >= positions.length) expandArray();
36
+
25
- positionList.add(p);
37
+ positions[length++] = p;
26
38
 
27
39
  } else {
28
40
 
@@ -32,9 +44,19 @@
32
44
 
33
45
  }
34
46
 
47
+ private void expandArray() {
48
+
49
+ Position[] newArray = new Position[positions.length + 5];
50
+
51
+ System.arraycopy(positions, 0, newArray, 0, length);
52
+
53
+ positions = newArray;
54
+
55
+ }
56
+
35
57
  private Position findPosition(Issue issue) {
36
58
 
37
- for(Position p : positionList) if(p.getIssue().equals(issue)) return p;
59
+ for(int i=0; i<length; i++) if(positions[i].getIssue().equals(issue)) return positions[i];
38
60
 
39
61
  return null;
40
62
 
@@ -42,7 +64,7 @@
42
64
 
43
65
  void printAll() {
44
66
 
45
- for(Position p : positionList) System.out.println(p);
67
+ for(int i=0; i<length; i++) System.out.println(positions[i]);
46
68
 
47
69
  }
48
70
 
@@ -96,6 +118,8 @@
96
118
 
97
119
  Issue(String code, String name) {
98
120
 
121
+ if(code == null || name == null) throw new NullPointerException();
122
+
99
123
  this.code = code;
100
124
 
101
125
  this.name = name;
@@ -150,6 +174,8 @@
150
174
 
151
175
  super(code, name);
152
176
 
177
+ if(market == null) throw new NullPointerException();
178
+
153
179
  this.market = market;
154
180
 
155
181
  }