0

我在 java 中编写了一个代码来交换 3*4 矩阵的列。

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

我想要的输出是:a,t,t,a,t,a,k,c,n,w,a,d。

所以我的循环如下:

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;
        }
    }
}

错误是:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Execute.swapColums(DtTwo.java:69)
at DtTwo.main(DtTwo.java:11)

请帮忙..

4

5 回答 5

1

例子:

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));
   }
}

输出:

[[a, t, t, a], [t, a, k, c], [n, w, a, d]]
于 2013-01-12T11:11:08.083 回答
0

你得到 java.lang.ArrayIndexOutOfBoundsException: -1 因为你有 4 列并且你在这里循环了更多的列

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

尝试改变

for(j=0;j<4;j++)
于 2013-03-01T11:17:55.727 回答
0

您可以使用以下代码:

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("]");
    }
}
于 2013-01-12T11:45:47.827 回答
0

这可能是一个愚蠢的想法,但它可以比现在简化得多,以使其更有效地使用

所以想法是,将一行转换为列表 -> 使其成为 -> 反转字符串 -> 使用拆分方法再次生成数组。

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(","));

结果 = a[1] -> t, a, k, c

于 2013-01-12T11:24:03.927 回答
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);
    }
}
于 2013-01-12T11:21:01.043 回答