Ive looked at other questions/answers on this website. however none seem to be solving this problem specifically. This is my code so far but I am > getting a
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at > > ColumnSorting.sortColumns(ColumnSorting.java:13) at > > TestColumnSorting.main(TestColumnSorting.java:19)
error when i run it.
public static int[][] sortColumns(int[][] matrix)
{
int tmp = 0;
int ct = 0;
for(int column = 0; column < matrix[ct].length; column++)
{
for(int row = 0; row < matrix.length; row++)
{
for (int i = row+1; i < matrix.length; i++)
{
if(matrix[row][column] > matrix[i][column])
{
tmp = matrix[row][column];
matrix[row][column] = matrix[i][column];
matrix[i][column] = tmp ;
}
}
}
ct++;
}
return matrix;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
You're getting this error because at the end of your for loops when you do ct++
you will eventually get ct=3
. Since array indexing starts at 0, your 3x3 column indices will go from 0-2. So when ct=3
and you do matrix[ct].length
you will get an array out of bounds error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.