私はオブジェクトの配列を持っています。それをスキャンして、見つかったオブジェクトが null でない限り、カウンターを 1 増やします。最初の null オブジェクトを見つけたら、ループを続行する理由がないため、for ループから抜け出したいと考えています。
次のコードを書きました。
// counter variable initialized to zero
int counter = 0;
// scan the array
for(int i = 0; i < this.array.length; i++) {
// as long as the object found is not null
while(!this.array[i].equals(null)) {
// increase the value of the counter by 1
counter += 1;
}
// when the first null object found, jump out of the loop
break;
}
for ループの i++ がマークされ、警告は Dead Code です。ただし、最初の null オブジェクトを見つけたときにループを停止するので、これは理にかなっていると思います。何も心配する必要はありませんか、それとも...?