i have written a code in java to swap columns for a 3*4 matrix.
String[][] a = { { a,t,t,a},
{ c,k,a,t},
{ d,a,w,n }
};
And what i want as the output is: a,t,t,a, t,a,k,c, n,w,a,d.
so my loop is as follows:
for(i=0;i<=2;i++) {
for(j=0;j<=5;j++) {
if(j<=2) {
temp=a[i][j+1];
a[i][j+1]=a[i][j];
a[i][j]=temp;
}
if(j>3) {
temp=a[i][j-3];
a[i][j-3]=a[i][j-4];
a[i][j-4]=temp;
} else {
temp=a[i][j-1];
a[i][j-1]=a[i][j-2];
a[i][j-2]=temp;
}
}
}
Error is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Execute.swapColums(DtTwo.java:69)
at DtTwo.main(DtTwo.java:11)
please help..