is there any other way to break this loop without using the actual break statement?
the thing is for some reason my professor doesn’t like that we use break in any other place that in the switch
i am having troubles in my method adding an object into the array of objects in the next empty space in the array
this is my code: (any other way to do it?)
public void add(Employee employeeObj)throws ArrayIndexOutOfBoundsException{
try{
for(int i= 0; i<MAX; i++){
if(listEmployee[i] == null){
listEmployee[i] = employeeObj;
break;
}
}
}catch(ArrayIndexOutOfBoundsException e){
throw new ArrayIndexOutOfBoundsException("ERROR!");
}
}
the method just need to add the employeObj into the array and if the case, thows an exception.
thanks for your help
try this:
but I see no problem with using breaks. There will always be circumstances where you want to stop processing a loop, and using a
break;makes much more sense (and makes it more readable!) than setting your loop counter up to a value that would make your loop stop at the next iteration.