【问题标题】:Get columns of a two-dimensional array and place them in a one-dimensional?获取二维数组的列并将它们放在一维中?
【发布时间】:2021-08-01 04:47:12
【问题描述】:
String myData[][] = {
        {"test1", "test2", "test3"},
        {"test1", "test2", "test3"},
        {"test1", "test2", "test3"},
};

如何将一个多维数组变成3个一维数组?

String myData1[] = {"test1", "test1", "test1"};
String myData2[] = {"test2", "test2", "test2"};
String myData3[] = {"test3", "test3", "test3"};

我需要得到

myData1 = "test1", "test1", "test1"
myData2 = "test2", "test2", "test2"
myData3 = "test3", "test3", "test3"

我用过

for (int i = 0; i < myData.length; i++) {
    for (int j = 0; j < myData.length; j++) {

【问题讨论】:

    标签: java arrays matrix multidimensional-array


    【解决方案1】:

    这是@khelwood 评论的可能实现。您在开始时创建空数组并将值分别放入其中。

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length; j++) {
            if (j == 0) {
                myData1[i] = myData[i][j];
            } else if (j == 1) {
                myData2[i] = myData[i][j];
            } else if (j == 2) {
                myData3[i] = myData[i][j];
            }
        }
    }
    

    这只有在你知道多维数组的大小时才有效

    【讨论】:

      【解决方案2】:

      最简单的方法就是在多维数组中取一个数组,如下所示:

      String myData [][] = {
          {"test1","test2","test3"},
          {"test1","test2","test3"},
          {"test1","test2","test3"},
      };
      
      String[] data1 = myData[0];
      String[] data2 = myData[1];
      String[] data3 = myData[3];
      

      这与@Narendra_Nath 的答案完全相同,但它更具可读性,我认为更便宜。

      【讨论】:

        【解决方案3】:

        您可以使用myData[index] 获取每一个,它应该返回“索引”上的数组

        例如:

        String myData[][] = {
                {"test1", "test2", "test3"},
                {"test1", "test2", "test3"},
                {"test1", "test2", "test3"},
        };
        String[] result = myData[0];
        // result should contain as value: { "test1", "test1", "test1" 
        

        【讨论】:

          【解决方案4】:

          您可以按如下方式使用

          /**
           * @param matrix rectangular two-dimensional array
           * @param m      number of columns
           * @param n      number of rows
           * @return the transposed matrix
           */
          public static String[][] matrixTranspose(String[][] matrix, int m, int n) {
              return IntStream.range(0, m)
                      .mapToObj(i -> IntStream.range(0, n)
                              .mapToObj(j -> matrix[j][i])
                              .toArray(String[]::new))
                      .toArray(String[][]::new);
          }
          
          public static void main(String[] args) {
              String[][] arr1 = {
                      {"test1", "test2", "test3", "test4"},
                      {"test1", "test2", "test3", "test4"},
                      {"test1", "test2", "test3", "test4"}};
          
              String[][] arr2 = matrixTranspose(arr1, 4, 3);
          
              // output
              Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
          }
          

          输出:

          [test1, test1, test1]
          [test2, test2, test2]
          [test3, test3, test3]
          [test4, test4, test4]
          

          【讨论】:

            猜你喜欢
            • 2013-12-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-30
            • 2020-08-26
            • 2022-01-23
            • 2016-10-28
            • 1970-01-01
            相关资源
            最近更新 更多