简体   繁体   中英

Another way to break a for loop with java?

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:

  for(int i= 0; i<MAX; i++){
    if(listEmployee[i] == null){
        listEmployee[i] = employeeObj;
        i=MAX;
    }

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.

You can simply do i = MAX or use a boolean flag if you don't have such a trivial situation.

As a personal note forbidding the usage of break outside switch statements doesn't make any sense.

Since your method is doing nothing else in this case, you can just use return instead of break :

public void add(Employee employeeObj)throws ArrayIndexOutOfBoundsException {
    try{
        for (int i= 0; i<MAX; i++) {
            if (listEmployee[i] == null) {
                listEmployee[i] = employeeObj;
                return;
            }

        }
    } catch(ArrayIndexOutOfBoundsException e) {
        throw new ArrayIndexOutOfBoundsException("ERROR!");
    }
}

But in general the other answers here are more applicable. That said, I would ask your professor to explain why break shouldn't be used - IMHO alternatives are often much less readable.

i am having troubles in my method adding an object into the array of objects in the next empty space in the array

I would suggest simply finding out the length of the array and inserting the object in the next location. No need to iterate the complete length of arrray.

public void add(Employee employeeObj){
           int n = listEmployee.length;
                listEmployee[n] = employeeObj;        
    }
}

Alternatively you could do it as

     int i = Arrays.asList(listEmployee).indexOf(null);
     if (i >= 0) {
         listEmployee[i] = employeeObj;
     }

note that Arrays.asList creates a view over array, not a new List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM