亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

假設列大小相同,如何在沒有數組副本的情況下在 Java 中附加和預先添加二維數組?

假設列大小相同,如何在沒有數組副本的情況下在 Java 中附加和預先添加二維數組?

紅顏莎娜 2022-06-23 16:16:26
我試過關注這個你 如何在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;**
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

需要考慮的重要一點是,當我們從第一個矩陣的最后一行開始時,我們希望保留該值,以便我們可以使用它將第二個矩陣的第一行到第 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

{

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

        }

    }


}


查看完整回答
反對 回復 2022-06-23
  • 1 回答
  • 0 關注
  • 91 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號