1

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

3
  • 3
    Setting i=MAX; will work, but honestly in this situation using break is definitely a better option. You could return as well, if the function has nothing else to do. Commented Feb 7, 2013 at 5:08
  • 3
    catching expection here seems useless, the code above would never throw an exception. Commented Feb 7, 2013 at 5:09
  • is there any way i could still use the ArrayIndexOutOfBoundsException exception usefull Commented Feb 7, 2013 at 5:18

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@PradeepSimha, excellent? It is just work around with worsened readability.
2

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.

3 Comments

Regarding your personal note, my professor finds that if he lets us use break, it results in some students trying to break from an if statement, and to keep things easier (not sure for who), he forbids it completely.
just to understand, what is the real reason behind that? am very new in java and i cant see the reason why!
Limiting the expressiveness of a language to avoid learning its semantics is just meaningless. Even because if you try to break outside an if statement you just get a compiler error. If your if is inside a switch/for/while then you get strange behavior but take for granted that every student, after doing the same mistake 10 times and spent time fixing it, will learn.
0

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.

Comments

0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.