将2d数组元素从一个复制到另一个

时间:2014-10-21 07:28:11

标签: java matrix

我想从n x n矩阵中找到adjugate矩阵,

例如矩阵是4x4

{0,  11, 15, 11},
{7,  0, 1 , 8},
{4, 19, 0 , 6},
{2, 3, 5, 0}  

这是我的输出

Matrix One

Loop: 1
0 1 8 19 0 6 3 5 0 
Loop: 2
7 1 8 4 0 6 2 5 0 
Loop: 3
7 0 8 4 19 6 2 3 0 
Loop: 4
7 0 1 4 19 0 2 3 5 
Loop: 5
11 15 11 19 0 6 3 5 0 
Loop: 6
0 15 11 4 0 6 2 5 0 
Loop: 7
0 11 11 4 19 6 2 3 0 
Loop: 8
0 11 15 4 19 0 2 3 5 
Loop: 9
11 15 11 0 1 8 3 5 0 
Loop: 10
0 15 11 7 1 8 2 5 0 
Loop: 11
0 11 11 7 0 8 2 3 0 
Loop: 12
0 11 15 7 0 1 2 3 5 
Loop: 13
11 15 11 0 1 8 19 0 6 
Loop: 14
0 15 11 7 1 8 4 0 6 
Loop: 15
0 11 11 7 0 8 4 19 6 
Loop: 16
0 11 15 7 0 1 4 19 0 

我将值存储到另一个2darray(matrixTwo)中以计算行列式以找到逆矩阵但发现存储在我的矩阵2中的内容与上面的输出相同。为什么值存储不同?请帮忙

Matrix Two
Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 2
7 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 3
4 19 0 6 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 4
2 3 5 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 6
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 9
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 11
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 13
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 14
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Loop: 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0      

我的代码

public class Matrix {
    public static void main(String argv[]) {
        int matrix[][]= { {0,  11, 15, 11},
                           {7,  0, 1 , 8},
                           {4, 19, 0 , 6},
                           {2, 3, 5, 0} 
        };

        int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length) * (matrix.length)];
        int ctr = 0;
        System.out.println("Matrix One");
        for(int i = 0; i < matrix.length; i++) {
            for(int j = 0; j < matrix.length; j++) {
                ctr++;
                System.out.println("\nLoop: " + ctr);
                for(int row = 0;row < matrix.length; row++) {
                    for(int col = 0; col < matrix[row].length; col++) {
                                    if(row != i && col != j) {
                                System.out.print(matrix[row][col] + " ");
                            matrixTwo[row][col] = matrix[row][col];
                    }
                }
            }           
        }
    }

    int ctrTwo = 0;
    System.out.println("\n\nMatrix Two");
    for(int row = 0; row < matrixTwo.length; row++) {
        ctrTwo++;
        System.out.println("Loop: " + ctrTwo);
        for(int col = 0; col < matrixTwo.length; col++) {
            System.out.print(matrixTwo[row][col] + " ");
        }
        System.out.println();
    }
}

2 个答案:

答案 0 :(得分:0)

现在,您只是将第一个矩阵复制到第二个矩阵中。

这里是你可以看到它的测试类:

public class test2 {

    int matrix[][] = { { 0, 11, 15, 11 }, { 7, 0, 1, 8 }, { 4, 19, 0, 6 },
            { 2, 3, 5, 0 } };

    int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length)
            * (matrix.length)];

    public test2() {
        int ctr = 0;
        System.out.println("Matrix One");
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix.length; col++) {
                ctr++;
                System.out.println("\nLoop: " + ctr);
                for (int row1 = 0; row1 < matrix.length; row1++) {
                    for (int col1 = 0; col1 < matrix[row1].length; col1++) {
                        if (row1 != row && col1 != col) {
                            System.out.print(matrix[row1][col1] + " ");
                            matrixTwo[row1][col1] = matrix[row1][col1];
                        }
                    }
                }
            }
        }

        int ctrTwo = 0;
        System.out.println("\n\nMatrix Two");
        for (int row = 0; row < matrixTwo.length; row++) {
            ctrTwo++;
            System.out.println("Loop: " + ctrTwo);
            for (int col = 0; col < matrixTwo.length; col++) {
                System.out.print(matrixTwo[row][col] + " ");
            }
            System.out.println();
        }
        printArray(matrix);
        System.out.println();
        printArray(matrixTwo);
    }

    public void printArray(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                System.out.printf("%3d", matrix[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String argv[]) {
        new test2();

    }
}

结果:

矩阵:

  0 11 15 11
  7  0  1  8
  4 19  0  6
  2  3  5  0

matrixTwo:

  0 11 15 11  0  0  0  0  0  0  0  0  0  0  0  0
  7  0  1  8  0  0  0  0  0  0  0  0  0  0  0  0
  4 19  0  6  0  0  0  0  0  0  0  0  0  0  0  0
  2  3  5  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

这不是你想做的事 想想在这一行中做了什么:matrixTwo[row][col] = matrix[row][col];

答案 1 :(得分:0)

这些是不同的,因为它们以不同的顺序打印

例如,

从矩阵中

Loop: 1
0 1 8 19 0 6 3 5 0 

打印

Loop: 1
matrix[1][1] = 0 matrix[1][2] = 1 matrix[1][3] = 8 matrix[2][1] = 19 matrix[2][2] = 0 matrix[2][3] = 6 matrix[3][1] = 3 matrix[3][2] = 5 matrix[3][3] = 0 

从matrixTwo

Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0 

打印

Loop: 1
matrixTwo[0][0] = 0 matrixTwo[0][1] = 11 matrixTwo[0][2] = 15 matrixTwo[0][3] = 11 matrixTwo[0][4] = 0 matrixTwo[0][5] = 0 matrixTwo[0][6] = 0 matrixTwo[0][7] = 0 matrixTwo[0][8] = 0 matrixTwo[0][9] = 0 matrixTwo[0][10] = 0 matrixTwo[0][11] = 0 matrixTwo[0][12] = 0 matrixTwo[0][13] = 0 matrixTwo[0][14] = 0 matrixTwo[0][15] = 0 

使用System.out.print("matrix[" + row + "][" + col + "] = " + matrix[row][col] + " ");。你可以看到差异