I have an array of objects. I want scan it and as long as the object I find is not null, increase a counter by 1. When I find the first null object, I want to jumb out of the for loop since there is no reason to continue looping.
I wrote the following code:
// 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;
}
The i++ in the for loop is marked and the warning is Dead Code. However, I guess this makes sense since when I find the first null object, I stop looping. So nothing to worry about, or ...?