假设列大小相同,如何在没有数组副本的情况下在 Java 中附加和预先添加二维数组?

我试过关注这个你 如何在java中正确附加两个二维数组? 删除所有数组副本,但出现问题。我还尝试了另一个指南,但只有在行相同的情况下才有效。


public int [][] appendMatrix(int[][]matrix, int [][] matrix2)

    {


        this.matrix = new int[matrix.length + matrix2.length][matrix[0].length];

        for(int i = 0; i < matrix.length; i++)

        {

            for(int j = 0; j < matrix[i].length; j++)

            {

                this.matrix[i][j] = matrix[i][j];

            }

            for(int j = matrix.length; j < matrix.length + matrix2.length; j++)

            {

                this.matrix[i][j]= matrix2[i-matrix.length][j];

            }   

        }

        return this.matrix;**


红颜莎娜
浏览 79回答 1
1回答

素胚勾勒不出你

需要考虑的重要一点是,当我们从第一个矩阵的最后一行开始时,我们希望保留该值,以便我们可以使用它将第二个矩阵的第一行到第 n 行添加到我们的结果矩阵中,而不会丢失轨道.package matrix;// I know you don't want to use imports, this is simply for testing purposes.import java.util.Arrays;public class MatrixAddition{&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int[][] matrix1 =&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1, 2, 3 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 4, 5, 6 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 7, 8, 9 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 10, 11, 12 } };&nbsp; &nbsp; &nbsp; &nbsp; int[][] matrix2 =&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 1, 1, 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { 2, 3, 4 } };&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Appending the two matrices results in: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.deepToString(twoDMatrixAppend(matrix1, matrix2)));&nbsp; &nbsp; &nbsp; &nbsp; printMatrix(twoDMatrixAppend(matrix1, matrix2));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nPrepending the two matrices results in: ");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.deepToString(twoDMatrixPrepend(matrix1, matrix2)));&nbsp; &nbsp; &nbsp; &nbsp; printMatrix(twoDMatrixPrepend(matrix1, matrix2));&nbsp; &nbsp; }&nbsp; &nbsp; private static int[][] twoDMatrixAppend(int[][] matrix1, int[][] matrix2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (matrix1[0].length != matrix2[0].length)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null; // Or throw new incompatible matrices exception&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int resultingRowLength = matrix1.length + matrix2.length; // The new length of the resulting matrix&nbsp; &nbsp; &nbsp; &nbsp; int[][] result = new int[resultingRowLength][matrix1[0].length];&nbsp; &nbsp; &nbsp; &nbsp; int currentRow, col, matrixTwoRowStart;&nbsp; &nbsp; &nbsp; &nbsp; for (currentRow = 0; currentRow < matrix1.length; currentRow++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (col = 0; col < matrix1[0].length; col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[currentRow][col] = matrix1[currentRow][col];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (matrixTwoRowStart = 0; matrixTwoRowStart < matrix2.length; matrixTwoRowStart++, currentRow++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (col = 0; col < matrix2[0].length; col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[currentRow][col] = matrix2[matrixTwoRowStart][col];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; private static int[][] twoDMatrixPrepend(int[][] matrix1, int[][] matrix2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return twoDMatrixAppend(matrix2, matrix1);&nbsp; &nbsp; }&nbsp; &nbsp; private static void printMatrix(int[][] arr)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; int row, col;&nbsp; &nbsp; &nbsp; &nbsp; for (row = 0; row < arr.length; row++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (col = 0; col < arr[0].length; col++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(String.format("%4d", arr[row][col]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java