0

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..

2
  • do you want to swap the columns or reverse some rows? your desired output is strange.
    – Juvanis
    Jan 12, 2013 at 11:03
  • If you want to understand what your code is doing and why it is not doing what you think it should, you should try your debugger first. Jan 12, 2013 at 11:42

5 Answers 5

1

Example:

public class Test
{  
   public static void main(String args[])
   {
       String[][] array2d = { 
                       { "a","t","t","a"},
                       { "c","k","a","t"},
                       { "d","a","w","n" }
                     };

       for (int i = 0; i < array2d.length; i++)
       {
           swapCols(array2d[i]);
       }

       print(array2d);
   }

   private static void swapCols(String s[])
   {
       for (int i = 0; i < s.length / 2; i++)
       {

            String temp = s[s.length - 1 - i];
            s[s.length - 1 - i] = s[i];
            s[i] = temp;
       }
   }

   private static void print(String[][] array2d)
   {
       System.out.println(Arrays.deepToString(array2d));
   }
}

Output:

[[a, t, t, a], [t, a, k, c], [n, w, a, d]]
0
0
public static void swap(int[][] arr, int line, int pos1, int pos2) {
    int temp = arr[line][pos1];
    arr[line][pos1] = arr[line][pos2];
    arr[line][pos2] = temp;
}

public static void main(String args[])
{
   String[][] arr = { 
                   { "a","t","t","a"},
                   { "c","k","a","t"},
                   { "d","a","w","n" }
                 };

    for (i=0; i<3; i++) {
        swap(arr, i, 0, 3);
        swap(arr, i, 1, 2);
    }
}
0

This might be a stupid idea, but it could be simplified much more than now to make it more efficient to use

So the idea is, convert a row into list -> make it to -> reverse the string -> use split method to generate the array again.

String[][] a = {{"a", "t", "t", "a"},
            {"c", "k", "a", "t"},
            {"d", "a", "w", "n"}
        };

        System.out.println(
            new StringBuffer(Arrays.asList(a[1]).
            toString().
            replace("[", "").
            replace("]", "").
            replace(" ", "")).
            reverse().
            toString().
            split(","));

result = a[1] -> t, a, k, c

0

You can use this code:

public void test() {
    String[][] a = {
            {"a", "t", "t", "a"},
            {"c", "k", "a", "t"},
            {"d", "a", "w", "n"}
    };

    String tmp;
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length / 2; j++) {
            tmp = a[i][j];
            a[i][j] = a[i][a.length - j];
            a[i][a.length - j] = tmp;
        }
    }

    for (int i = 0; i < a.length; i++) {
        System.out.print("[");
        for (int j = 0; j < a[i].length; j++) {
            System.out.print(a[i][j] + ",");
        }
        System.out.println("]");
    }
}
0

You get java.lang.ArrayIndexOutOfBoundsException: -1 because you have 4 columns and you loop over more columns here

for(j=0;j<=5;j++)

Try to change in

for(j=0;j<4;j++)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.