0

Hi there

I'm trying to check if an array area exists, e.g.

if (array[99][99].exists){ ....}

THis doesn't work... e.g. It gives me an arrayIndexOutOfBounds error.

What I need for it to do is just ignore and carry on the code. If it finds an arrayIndexOutOfBounds, fine, carry on, just ignore.

Try/catch may not work in this situation. There are multiple conditions, some true and some false, that it must go through, so if if using a try/catch, it will get out of the forloop which checks...

Please help.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by masijade
0

Either wrap the stuff in a try catch block, or simply check the lengths of the arrays. I.E. if checking whether array[99][99] exists (and is a valid object and not null) simply do

if ((array.length >= 100) && (array[99].length >= 100) && (array[99][99] != null))
0

or you could write the try catch inside the for loop as well. m not sure whether this is good programming practice.

0

Of course, if you don't want to make the three checks on the if line, simply write a method

boolean exists(Object[] array, int index1, int index2) {
    return ((array.length >= 100) && (array[99].length >= 100) && (array[99][99] != null)/* && (add whatever additional condition you want)*/);
}

and call it instead

if (exists(array, 99, 99)) {

Edited by masijade: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.