【问题标题】:How to append and prepend 2D arrays in Java without Array copy assuming the column sizes are the same?假设列大小相同,如何在没有数组副本的情况下在 Java 中附加和预先添加二维数组?
【发布时间】:2019-02-18 03:09:46
【问题描述】:

我试过关注这个How do you append two 2D array in java properly? 删除所有数组副本,但出了点问题。我还尝试了另一个指南,但只有在行相同时才有效。

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

【问题讨论】:

    标签: java arrays matrix append


    【解决方案1】:

    要考虑的重要一点是,当我们从第一个矩阵的最后一行开始时,我们希望保留该值,以便我们可以使用它将第二个矩阵的第一行到第 n 行添加到我们的结果矩阵中,不迷路。

    示例输出:output as an array and visualized as a matrix

    package matrix;
    
    // I know you don't want to use imports, this is simply for testing purposes.
    import java.util.Arrays;
    
    public class MatrixAddition
    {
        public static void main(String[] args)
        {
            int[][] matrix1 =
            {
                    { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 },
                    { 10, 11, 12 } };
            int[][] matrix2 =
            {
                    { 1, 1, 1 },
                    { 2, 3, 4 } };
    
    
            System.out.println("Appending the two matrices results in: ");
            System.out.println(Arrays.deepToString(twoDMatrixAppend(matrix1, matrix2)));
            printMatrix(twoDMatrixAppend(matrix1, matrix2));
    
            System.out.println("\nPrepending the two matrices results in: ");
            System.out.println(Arrays.deepToString(twoDMatrixPrepend(matrix1, matrix2)));
            printMatrix(twoDMatrixPrepend(matrix1, matrix2));
        }
    
    
        private static int[][] twoDMatrixAppend(int[][] matrix1, int[][] matrix2)
        {
            if (matrix1[0].length != matrix2[0].length)
            {
                return null; // Or throw new incompatible matrices exception
            }
    
            int resultingRowLength = matrix1.length + matrix2.length; // The new length of the resulting matrix
    
            int[][] result = new int[resultingRowLength][matrix1[0].length];
    
            int currentRow, col, matrixTwoRowStart;
            for (currentRow = 0; currentRow < matrix1.length; currentRow++)
            {
                for (col = 0; col < matrix1[0].length; col++)
                {
                    result[currentRow][col] = matrix1[currentRow][col];
                }
            }
    
            for (matrixTwoRowStart = 0; matrixTwoRowStart < matrix2.length; matrixTwoRowStart++, currentRow++)
            {
                for (col = 0; col < matrix2[0].length; col++)
                {
                    result[currentRow][col] = matrix2[matrixTwoRowStart][col];
                }
            }
    
            return result;
        }
    
        private static int[][] twoDMatrixPrepend(int[][] matrix1, int[][] matrix2)
        {
            return twoDMatrixAppend(matrix2, matrix1);
        }
    
        private static void printMatrix(int[][] arr)
        {
            System.out.println();
            int row, col;
            for (row = 0; row < arr.length; row++)
            {
                for (col = 0; col < arr[0].length; col++)
                {
                    System.out.print(String.format("%4d", arr[row][col]));
                }
                System.out.println();
            }
        }
    
    }
    

    【讨论】:

    • 不,我只需要将二维数组矩阵放在一起,因此矩阵 a:1 2 3 4 5 6 7 8 9 10 11 12(4x3 矩阵)和 1 1 1 2 3 4(2x3 矩阵) 是一个矩阵,所以 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2 3 4 (6x3 matrx) @Paper-SSheets
    • 啊,就这样? (不能包含图片,所以我想使用此链接)symbolab.com/solver/inequalities-calculator/…
    • 是的,就像@Paper-SSheets
    • 太棒了,给我几分钟,我正在研究解决方案和测试。
    • 我将如何做同样的事情? @Paper-SSheets
    猜你喜欢
    • 2020-03-11
    • 2021-02-22
    • 2022-12-10
    • 1970-01-01
    • 2018-08-05
    • 2020-05-24
    • 1970-01-01
    • 2023-01-13
    • 2013-12-08
    相关资源
    最近更新 更多