回答編集履歴
1
改正
answer
CHANGED
@@ -48,4 +48,75 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
}
|
51
|
+
```
|
52
|
+
|
53
|
+
コンストラクタの引数ごとに配列の長さを変えられるようにしたのが以下です
|
54
|
+
```java
|
55
|
+
class Test {
|
56
|
+
private int number = 1;
|
57
|
+
int[] x = new int[number];
|
58
|
+
int[] y = new int[number];
|
59
|
+
private int a = 10;
|
60
|
+
private int b = 30;
|
61
|
+
|
62
|
+
public Test(String name) {
|
63
|
+
|
64
|
+
switch (name) {
|
65
|
+
case "first":
|
66
|
+
this.number = 5;
|
67
|
+
x = new int[number];
|
68
|
+
for (int i = 0; i < number; i++) {
|
69
|
+
this.x[i] = a;
|
70
|
+
}
|
71
|
+
y = new int[number];
|
72
|
+
this.y[0] = b;
|
73
|
+
for (int i = 1; i < number; i++) {
|
74
|
+
this.y[i] = this.y[i - 1] + b;
|
75
|
+
}
|
76
|
+
break;
|
77
|
+
case "second":
|
78
|
+
this.number = 8;
|
79
|
+
x = new int[number];
|
80
|
+
for (int i = 0; i < number; i++) {
|
81
|
+
this.x[i] = a * 2;
|
82
|
+
}
|
83
|
+
y = new int[number];
|
84
|
+
this.y[0] = b * 3;
|
85
|
+
for (int i = 1; i < number; i++) {
|
86
|
+
this.y[i] = this.y[i - 1] + b * 3;
|
87
|
+
}
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
class E2 {
|
94
|
+
|
95
|
+
public static void main(String[] args) {
|
96
|
+
|
97
|
+
Test a = new Test("first");
|
98
|
+
Test b = new Test("second");
|
99
|
+
|
100
|
+
for (int c : a.x) {
|
101
|
+
System.out.print(c + "-");
|
102
|
+
}
|
103
|
+
System.out.println();
|
104
|
+
|
105
|
+
for (int c : a.y) {
|
106
|
+
System.out.print(c + "-");
|
107
|
+
}
|
108
|
+
System.out.println();
|
109
|
+
|
110
|
+
for (int c : b.x) {
|
111
|
+
System.out.print(c + "-");
|
112
|
+
}
|
113
|
+
System.out.println();
|
114
|
+
|
115
|
+
for (int c : b.y) {
|
116
|
+
System.out.print(c + "-");
|
117
|
+
}
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
51
122
|
```
|