質問編集履歴
2
質問内容を変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,62 +1,1 @@
|
|
1
|
-
](6615aacc2a25cda122814157110001ad.png)
|
2
|
-
学校の課題でカスタム配列クラスの問題に取り掛かっています。
|
3
|
-
画像に書かれている事柄をJava言語に置き換える問題です。
|
4
|
-
以下では、メソッドのindexOfまで作りましたが、その後のremoveメソッドだけ出来ないです。
|
5
|
-
|
1
|
+
配列に格納している要素をremoveメソッドを使って、全ての要素を削除するためにはどうしたらよいですか?
|
6
|
-
|
7
|
-
public class IntCollector {
|
8
|
-
private int[] array;
|
9
|
-
|
10
|
-
public IntCollector() {
|
11
|
-
this.array = new int[0];
|
12
|
-
}
|
13
|
-
|
14
|
-
public IntCollector(int[] values) {
|
15
|
-
/*this.array = new int[values.length];
|
16
|
-
for(int i = 0; i < values.length; i++){
|
17
|
-
this.array[i] = values[i];
|
18
|
-
}*/
|
19
|
-
this.setArray(values);
|
20
|
-
}
|
21
|
-
|
22
|
-
public int[] getArray() {
|
23
|
-
return array;
|
24
|
-
}
|
25
|
-
|
26
|
-
public void setArray(int[] values) {
|
27
|
-
this.array = array;
|
28
|
-
this.array = new int[values.length];
|
29
|
-
for (int i = 0; i < values.length; i++) {
|
30
|
-
this.array[i] = values[i];
|
31
|
-
}
|
32
|
-
|
33
|
-
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
public void add(int value) {
|
38
|
-
//今現在の配列の要素数+1で新しい配列を作成
|
39
|
-
int[] newArray = new int[this.array.length + 1];
|
40
|
-
//今現在の配列の値を全て新しい配列にコピー
|
41
|
-
for (int i = 0; i < array.length; i++) {
|
42
|
-
newArray[i] = this.array[i];//this.array[i]:要素を1個足してない状態
|
43
|
-
}
|
44
|
-
//新しい配列の末尾に引数の値で要素を追加
|
45
|
-
newArray[newArray.length - 1] = value;
|
46
|
-
//新しい配列を今現在の配列とする
|
47
|
-
this.array = newArray;
|
48
|
-
}
|
49
|
-
public int countOf(int value) {
|
50
|
-
int count = 0;
|
51
|
-
for (int v : array) {
|
52
|
-
if (v == value)
|
53
|
-
count++;
|
54
|
-
}
|
55
|
-
|
56
|
-
public int indexOf ( int value){
|
57
|
-
for (int i = 0; i < array.length; i++) {
|
58
|
-
if (array[i] == value)
|
59
|
-
return i;
|
60
|
-
}
|
61
|
-
return -1;
|
62
|
-
}
|
1
removeメソッドの部分で具体的に分からないことを書き直しました
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
removeメソッドの書き方で困っています
|
body
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
](6615aacc2a25cda122814157110001ad.png)
|
2
|
+
学校の課題でカスタム配列クラスの問題に取り掛かっています。
|
2
3
|
画像に書かれている事柄をJava言語に置き換える問題です。
|
3
4
|
以下では、メソッドのindexOfまで作りましたが、その後のremoveメソッドだけ出来ないです。
|
4
|
-
addメソッドの逆を書けばいいのですがよ
|
5
|
+
addメソッドの逆を書けばいいのですが配列要素の切り詰めや削除をどのように書けば良いですか?
|
5
|
-
もしよろしければ、removeメソッドのみソースコードを書いてもらえますか?
|
6
6
|
|
7
7
|
public class IntCollector {
|
8
8
|
private int[] array;
|