Pisike007
Pisike007

Reputation: 101

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

Upvotes: 1

Views: 585

Answers (5)

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

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

Upvotes: 0

Shurmajee
Shurmajee

Reputation: 1047

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;        
    }
}

Upvotes: 0

Paul Bellora
Paul Bellora

Reputation: 55233

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.

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

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.

Upvotes: 3

Jack
Jack

Reputation: 133669

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.

Upvotes: 2

Related Questions