# Thread: Array Index Out of Bound Exception

1. Member
Join Date
Dec 2014
Posts
19
Rep Power
0

## Array Index Out of Bound Exception

When i am trying to execute this code it is showing
Java Code:
```class Sort2D
{

public static void main(String...s)
{
Sort2D r=new Sort2D();
int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{446,876,23,546565,332}});
for(int i=0;i<z.length;i++)
for(int j=0;j<z[i].length;j++)
System.out.println(z[i][j]);
}

int[][] sort(int x[][])
{int temp;
for(int i=0;i<x.length;i++)
{
for(int j=0;j<(x[i].length)-i;j++)
{
if(x[i][j]<x[i][j-1])
{
temp=x[i][j];
x[i][j]=x[i][j-1];
x[i][j-1]=temp;
}
}
}
return x;
}
}```
Last edited by iamsethi; 12-15-2014 at 01:09 PM.

2. ## Re: Array Index Out of Bound Exception

Please copy the full text of the error message and paste it here. It has important info about the error.

Also please edit your post and wrap the code in code tags.

3. ## Re: Array Index Out of Bound Exception

If j equals zero, j-1 equals minus one ...

kind regards,

Jos

4. Member
Join Date
Dec 2014
Posts
19
Rep Power
0

## Re: Array Index Out of Bound Exception

wow thanks! a lot.it worked

5. ## Re: Array Index Out of Bound Exception

Originally Posted by iamsethi
wow thanks! a lot.it worked

Jos

6. Member
Join Date
Dec 2014
Posts
19
Rep Power
0

## Re: Array Index Out of Bound Exception

Correct Code:
Java Code:
```class Sort2D
{

public static void main(String...s)
{
Sort2D r=new Sort2D();
int z[][]=r.sort(new int[][]{{1,0,567,4,3,33},{333,677,34243,8987,3434,324},{446,876,23,546565,332}});
for(int i=0;i<z.length;i++)
for(int j=0;j<z[i].length;j++)
System.out.println(z[i][j]);
}

int[][] sort(int x[][])
{int temp;
for(int i=0;i<x.length;i++)
{
for(int j=1;j<(x[i].length)-i;j++)
{
if(x[i][j]<x[i][j-1])
{
temp=x[i][j];
x[i][j]=x[i][j-1];
x[i][j-1]=temp;
}
}
}
return x;
}
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•