I was trying to set all the elements to null in an array of a defined class. I have just learned the usage of a for-each loop, so I tried the following:
for(MyClass element:array){
element=null;
}
however this did not work after compiling, and there is a warning "The value of the local variable element is not used". I tried a normal for loop instead and it worked as expected:
for(int i=0;i<array.length;i++){
array[i]=null;
}
My question is that why didn't the for-each loop work? Do I have some misunderstanding regarding its usage?