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.

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))

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

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)) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.