user4047127
user4047127

Reputation: 57

ArrayIndexOutOfBoundsException in multi dimentional array

I am getting the following Exception in thread "main" : java.lang.ArrayIndexOutOfBoundsException: 2 error:

My code:

int ia=445;
int ja=445;

double [][][]ma=new double [445][445][2];
ma=na;
for (int kk=1;kk<=2;kk++)
{
   int jj=2;
   if (kk == 2)
   {
     jj=ja;
   }
   for (int ii=2;ii<ia-1;ii++)
   { 
    double uu=0.5*abs(ma[ii][jj][2]+ma[ii-1][jj][2]);
    System.out.println(uu);

   }
}

Though there are no ArrayIndexOutOfBoundsException issues, I am still getting this exception. Can some one explain how to fix this issue?

Upvotes: 0

Views: 53

Answers (2)

Eran
Eran

Reputation: 393781

ma[ii-1][jj][2] will give you an exception regardless of the values of ii and jj, since the last index must be either 0 or 1 (since the dimentions are [445][445][2]).

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

The problem is here:

double uu=0.5*abs(ma[ii][jj][2]+ma[ii-1][jj][2]);
                             ^               ^
                             here            here too

You can only access to indexes 0 and 1 in the third dimension of your array.

Maybe you want/need:

double uu=0.5*abs(ma[ii][jj][1]+ma[ii-1][jj][1]);

or

double uu=0.5*abs(ma[ii][jj][0]+ma[ii-1][jj][0]);

Upvotes: 1

Related Questions