【问题标题】:How do you append two 2D array in java properly?你如何在java中正确附加两个二维数组?
【发布时间】:2011-08-14 19:43:43
【问题描述】:

我一直在尝试在 java 中附加两个二维数组。是否可以举个例子,因为我一直在尝试查找但找不到。

int [][]appendArray(empty,window)
{
    int [][]result= new int [empty.length][empty[0].length+window[0].length];       
}

【问题讨论】:

  • 什么是“空”和“窗口”?你的方法签名是 int[][] appendArray(int[][] empty, int[][] window) 吗?

标签: java arrays append


【解决方案1】:

单线带流

int[][] combi = Stream.concat( Arrays.stream( a ), Arrays.stream( b ) ).toArray( int[][]::new );

二维Object数组here的对应题

【讨论】:

    【解决方案2】:

    如果我理解正确,您想在与 DomS 和 MeBigFatGuy 所想的相反的维度上附加它。如果我是正确的,有两种方法:


    如果“列”高度(二维的长度)在每个数组中是固定的,则可以使用此方法。如果数组的第一维长度不同,它会留下空白(零填充)单元格。为了让这段代码更安全,你可能想要

    /**
     * For fixed "column" height. "Blank cells" will be left, if the two arrays have different "width" 
     */
    static int[][] appendArray2dFix(int[][] array1, int[][] array2){
        int a = array1[0].length, b = array2[0].length;
    
        int[][] result = new int[Math.max(array1.length,array2.length)][a+b];
    
        //append the rows, where both arrays have information
        int i;
        for (i = 0; i < array1.length && i < array2.length; i++) {
            if(array1[i].length != a || array2[i].length != b){
                throw new IllegalArgumentException("Column height doesn't match at index: " + i);
            }
            System.arraycopy(array1[i], 0, result[i], 0, a);
            System.arraycopy(array2[i], 0, result[i], a, b);
        }
    
        //Fill out the rest
        //only one of the following loops will actually run.
        for (; i < array1.length; i++) {
            if(array1[i].length != a){
                throw new IllegalArgumentException("Column height doesn't match at index: " + i);
            }
            System.arraycopy(array1[i], 0, result[i], 0, a);
        }
    
        for (; i < array2.length; i++) {
            if(array2[i].length != b){
                throw new IllegalArgumentException("Column height doesn't match at index: " + i);
            }
            System.arraycopy(array2[i], 0, result[i], a, b);
        }
    
        return result;
    }
    

    如果您希望允许每个数组中的列不同,这是可能的,只需稍作更改。这不会留下任何空单元格。

    /**
     * For variable "column" height. No "blank cells"
     */
    static int[][] appendArray2dVar(int[][] array1, int[][] array2){
    
        int[][] result = new int[Math.max(array1.length,array2.length)][];
    
        //append the rows, where both arrays have information
        int i;
        for (i = 0; i < array1.length && i < array2.length; i++) {
            result[i] = new int[array1[i].length+array2[i].length];
            System.arraycopy(array1[i], 0, result[i], 0, array1[i].length);
            System.arraycopy(array2[i], 0, result[i], array1[i].length, array2[i].length);
        }
    
        //Fill out the rest
        //only one of the following loops will actually run.
        for (; i < array1.length; i++) {
            result[i] = new int[array1[i].length];
            System.arraycopy(array1[i], 0, result[i], 0, array1[i].length);
        }
    
        for (; i < array2.length; i++) {
            result[i] = new int[array2[i].length];
            System.arraycopy(array2[i], 0, result[i], 0, array2[i].length);
        }
    
        return result;
    }
    

    从 DomS 修改的测试代码

    public static void main(String[] args) {
    
        //Test Var
    
        int[][] array1 = new int[][] {
                {1, 2, 3},
                {3, 4, 5, 6},
        };
        int[][] array2 = new int[][] {
                {11, 12, 13,14 },
                {13, 14, 15, 16, 17},
        };
    
        int[][] expected = new int[][] {
                {1, 2, 3, 11, 12, 13, 14},
                {3, 4, 5, 6, 13, 14, 15, 16, 17}
        };
    
    
        int[][] appended = appendArray2dVar(array1, array2);
        System.out.println("This");
        for (int i = 0; i < appended.length; i++) {
            for (int j = 0; j < appended[i].length; j++) {
                System.out.print(appended[i][j]+", ");
            }
            System.out.println();
        }
        System.out.println("Should be the same as this");
        for (int i = 0; i < expected.length; i++) {
            for (int j = 0; j < expected[i].length; j++) {
                System.out.print(expected[i][j]+", ");
            }
            System.out.println();
        }
    
    
        //Test Fix
        array1 = new int[][] {
                {1, 2, 3, 4},
                {3, 4, 5, 6},
        };
        array2 = new int[][] {
                {11, 12, 13},
                {13, 14, 15},
        };
    
       expected = new int[][] {
                {1, 2, 3, 4,11, 12, 13},
                {3, 4, 5, 6, 13, 14, 15}
        };
    
    
        appended = appendArray2dFix(array1, array2);
        System.out.println("This");
        for (int i = 0; i < appended.length; i++) {
            for (int j = 0; j < appended[i].length; j++) {
                System.out.print(appended[i][j]+", ");
            }
            System.out.println();
        }
        System.out.println("Should be the same as this");
        for (int i = 0; i < expected.length; i++) {
            for (int j = 0; j < expected[i].length; j++) {
                System.out.print(expected[i][j]+", ");
            }
            System.out.println();
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      给你:

      import java.util.Arrays;
      
      
      public class Array2DAppend {
      
          public static void main(String[] args) {
      
              int[][] a = new int[][] {{1, 2}, {3, 4}};
              int[][] b = new int[][] {{1, 2, 3}, {3, 4, 5}};
      
              System.out.println(Arrays.deepToString(a));
              System.out.println(Arrays.deepToString(b));
              System.out.println(Arrays.deepToString(append(a, b)));
      
          }
      
          public static int[][] append(int[][] a, int[][] b) {
              int[][] result = new int[a.length + b.length][];
              System.arraycopy(a, 0, result, 0, a.length);
              System.arraycopy(b, 0, result, a.length, b.length);
              return result;
          }
      }
      

      和输出:

      [[1, 2], [3, 4]]
      [[1, 2, 3], [3, 4, 5]]
      [[1, 2], [3, 4], [1, 2, 3], [3, 4, 5]]
      

      【讨论】:

        【解决方案4】:

        如果我正确理解了您的问题,此方法会将两个二维数组附加在一起 ​​-

        private static int[][] appendArrays(int[][] array1, int[][] array2) {
            int[][] ret = new int[array1.length + array2.length][];
            int i = 0;
            for (;i<array1.length;i++) {
                ret[i] = array1[i];
            }
            for (int j = 0;j<array2.length;j++) {
                ret[i++] = array2[j];
            }
            return ret;
        }
        

        这个快速的代码将测试它 -

                int[][] array1 = new int[][] {
                    {1, 2, 3},
                    {3, 4, 5, 6},
            };
            int[][] array2 = new int[][] {
                    {11, 12, 13},
                    {13, 14, 15, 16},
            };
        
            int[][] expected = new int[][] {
                    {1, 2, 3},
                    {3, 4, 5, 6},
                    {11, 12, 13},
                    {13, 14, 15, 16},
            };
        
        
            int[][] appended = appendArrays(array1, array2);
            System.out.println("This");
            for (int i = 0; i < appended.length; i++) {
                for (int j = 0; j < appended[i].length; j++) {
                    System.out.print(appended[i][j]+", ");
                }
                System.out.println();
            }
            System.out.println("Should be the same as this");
            for (int i = 0; i < expected.length; i++) {
                for (int j = 0; j < expected[i].length; j++) {
                    System.out.print(expected[i][j]+", ");
                }
                System.out.println();
            }
        

        【讨论】:

          【解决方案5】:

          我猜“附加”是指用另一个矩阵的行来扩展矩阵的行数?在这种情况下,两个数组/矩阵必须有相同的列数! 因此,例如,您可以将 a[7][6] 与 b[100][6] 附加,这将通过简单地将 b 的 100 行附加到 a 的 7 行来生成数组 c[107][6] ——但这仅仅是因为它们两者都有 6 列。例如,将 a[7][3] 附加到 b[100][6] 是没有意义的! 所以你的功能必须预先强制执行这些。 不,如果不编写自己的代码,Java 就无法做到这一点:

          int[][] appendArray( empty, window ) {
           if( empty[0].length != window[0].length ) throw new IllegalArgumentException( "Wrong column size" );
           int[][] result = new int[empty.length + window.length];
           for( int i = 0; i < empty.length; i++ )
            System.arrayCopy( empty[i], 0, result[0], 0, empty[i].length );
           for( int i = 0; i < window.length; i++ )
            System.arrayCopy( window[i], 0, result[i + empty.length], 0, window[i].length );
           return result;
          }
          

          【讨论】:

          • 完全不真实。 Java 数组没有这样的要求。
          • 不是Java有没有这样的要求!从逻辑上讲,附加 2 个数组一个 3 列和一个 5 列是没有任何意义的——因为额外的 2 列会去哪里?你能帮我解开这个谜吗——用 java 或任何其他语言?
          • 您在考虑这些数组时过于笼统。把它们想象成一个 Lisp 列表结构。我有两个购物清单:一个是衣服,有衬衫、鞋子、领带。在上面。另一个是食物,有冰淇淋、牛排、土豆、面包。我可以很容易地将这两个列表合二为一,与(布料(衬衫、鞋子、领带)、食物(冰淇淋、牛排、土豆、面包))第二维长度不一致的事实无关紧要。请参阅我的答案以了解如何对其进行编码。
          • 最后多维数组只是一维数组,其中元素恰好是数组本身。
          • 我的教授有这些几乎不可能完成的项目。我正在转学,因为另一所学校有更多的帮助资源,并与另一位 Comp sci 教授交谈,而项目不是这样的
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-24
          • 1970-01-01
          • 2012-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多