Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Sorting 2D Array problem

1. ## Sorting 2D Array problem

Hi, I am using a bubble sort algorithm to sort a 2D array. The array is as follows:

```String data[][] = {{"John", "Smith", "14.75", "30", "ZZZZ", "ZZZZ", "ZZZZ",},{"Bill", "Gates", "18.50", "40","ZZZZ", "ZZZZ", "ZZZZ",},
{"Steve","Jobs", "10.75", "25","ZZZZ", "ZZZZ", "ZZZZ",},{"Paul", "Allen","21.50","34","ZZZZ", "ZZZZ", "ZZZZ",},
{"Steve", "Ballmer", "18.75", "35","ZZZZ", "ZZZZ", "ZZZZ",},{"ZZZZ", "ZZZZ", "ZZZZ", "ZZZZ"}};
//ZZZZ are marker values,the last row is to hold total values of all employees```

I want to sort this 2D array by comparing last names (index 1), and bubble sorting all their corresponding values along with it.

```static public String[][] sort(String data[][])
{
for(int x = 0; x <= data.length-1; x++)
{
for(int y = 0; y<=data.length-2;y++)
{
if(data[y][1].compareTo(data[y+1][1]) > 0)   //compare lastnames alphabetically
{
String temp = data[y][1];    //swap last names
data[y][1]=data[y+1][1];
data[y+1][1] = temp;

String temp2 = data[y][0];  //swap first names
data[y][0]=data[y+1][0];
data[y+1][0] = temp2;

String temp3 = data[y][2];  //etc
data[y][2]=data[y+1][2];
data[y+1][2] = temp3;

String temp4 = data[y][3];
data[y][3]=data[y+1][3];
data[y+1][3] = temp4;

String temp5 = data[y][4];
data[y][4]=data[y+1][4];
data[y+1][4] = temp5;

String temp6 = data[y][5];
data[y][5]=data[y+1][5];
data[y+1][5] = temp6;

}
}

}```

It compiles but when I run it i get the error:
java.lang.ArrayIndexOutOfBoundsException: 4

2. ## Re: Sorting 2D Array problem

.ArrayIndexOutOfBoundsException
Array indexes range from 0 the the array length-1
Check the code to see which index is going past the end of the array.
If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

If you had copied the full text of the error message it would show the line number of the error and the value of the index.

Hint: the second dim can be handled as a single dim array
```int[][] twoDim = {{1,2,3}, {4,5,6}};
//  swap the one dim array parts
int[] oneDimTemp = twoDim[0];  //  save
twoDim[0] = twoDim[1];  // move
twoDim[1] = oneDimTemp;
System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]```

3. ## The Following User Says Thank You to Norm For This Useful Post:

lordofrandom (May 4th, 2013)

4. ## Re: Sorting 2D Array problem

Originally Posted by Norm
Array indexes range from 0 the the array length-1
Check the code to see which index is going past the end of the array.
If you can't see, add some println statements that print out the values of the indexes. The print out will show you what index is too big.

If you had copied the full text of the error message it would show the line number of the error and the value of the index.

Hint: the second dim can be handled as a single dim array
```int[][] twoDim = {{1,2,3}, {4,5,6}};
//  swap the one dim array parts
int[] oneDimTemp = twoDim[0];  //  save
twoDim[0] = twoDim[1];  // move
twoDim[1] = oneDimTemp;
System.out.println(Arrays.deepToString(twoDim));// [[4, 5, 6], [1, 2, 3]]```
Thanks a lot, sir.