-1
ArrayListはこの仕事をはるかに単純化しますが、これは配列のためです。どのように特定の配列要素をnullに設定し、それらの要素を配列の末尾に移動するのですか?
これは、これまでの私の方法です:
public boolean remove(String name) {
int temp = 0;
for (int i = 0; i<counter; i++) {
if (friends[i].equals(name)) {
friends[i]=null;
for (int j = i; j > counter; j++) {
friends[j] = friends[j+1];
}
return true;
}
}
return false;
}
私が望む結果は次のとおりです。
String[] friends = {"Sean", "James", "Andrew", "Garfield", "Patrick"};
myfriends.remove("James");
System.out.println(Arrays.toString(friends));
コンソール出力:Sean, Andrew, Garfield,Patrick, null
ハハ! ofcそれはj
JohnBanana
@ JohnBananaデバッガでは、コードが決してループに入っていないことがわかります。 –
ループに入ることはありません。 – JohnBanana