【问题标题】:Move null elements of each row in a 2D array to the end of that row将二维数组中每一行的空元素移动到该行的末尾
【发布时间】:2021-01-20 10:46:50
【问题描述】:

假设我有一个如下所示的二维数组:

[[O, X, null, O, O, null, null], [null, null, O, null, null, O, O]]

我希望它看起来像这样:

[[O, X, O, O, null, null, null], [O, O, O, null, null, null, null]]

我试过了,但它不起作用:

String[][] a = new String[row][col];
String[][] b = new String[row][col];

for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a.length; j++) {
        if (a[i][j] != null) {
            a[i][j] = b[i][j];
        } else {
            b[i][j] = a[i][j + 1];
        }
    }
}

【问题讨论】:

    标签: java arrays sorting multidimensional-array null


    【解决方案1】:

    让我印象深刻的第一件事是你从不检查内部数组的长度。给定示例,它只会检查前 2 个元素。

    // Before
    for (int j = 0; j < status.length; j++)
    
    // Solution
    for (int j = 0; j < status[i].length; j++)
    

    至于具体问题,您可以通过跟踪第一个 null 的索引并将遇到的任何元素移动到它来解决。

    int lastEmpty = 0;
    
    for (int j = 0; j < status[i].length; j++) {
        // Move to the best compressed position
        if (status[i][j] != null && status[i][lastEmpty] == null) {
            status[i][lastEmpty] = status[i][j];
            status[i][j] = null;
        }
    
        // Shift to always be the first null
        if (status[i][lastEmpty] != null) {
            lastEmpty++;
        }
    }
    
    

    【讨论】:

      【解决方案2】:

      解决问题的一些要点

      • 内循环需要使用a[i].length
      • a[i][j] = b[i][j]; 这里你需要在b 中赋值a
      • 您需要为b 二维数组的每一行使用计数器索引,并且在存储后发现非空值时将其递增,剩余索引默认为空值。
      for (int i = 0; i < a.length; i++) {
        int indNew = 0; // Index for every row
        for (int j = 0; j < a[i].length; j++) {
          if (a[i][j] != null) {
            b[i][indNew] = a[i][j]; // store not null value
            indNew++;  // increase after storing
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        您不需要创建新的二维数组来存储其更新版本,null 的值可以简单地移动到每行的末尾,然后使用Arrays.fill 从计算的索引开始设置 null。

        更新:方法可以是通用的

        public static <T> void moveNulls(T[][] status) {
            for (T[] row : status) {
                if (null == row) {
                    continue;
                }
                int id = 0;
                for (int i = 0; i < row.length; i++) {
                    if (row[i] != null) {
                        row[id++] = row[i];
                    }
                }
                Arrays.fill(row, id, row.length, null);
            }
        }
        

        测试

        String[][] status = {
                {"aaa", null, "bbb", null, null, "ccc"},
                {null, "bbb", null, null, "ccc", "ddd", null, "eee"},
                {null, null, "fff", null, "ccc", "ddd", null, "ggg", null},
        };
        
        moveNulls(status);
        for (String[] row : status) {
            System.out.println(Arrays.toString(row));
        }
        

        输出

        [aaa, bbb, ccc, null, null, null]
        [bbb, ccc, ddd, eee, null, null, null, null]
        [fff, ccc, ddd, ggg, null, null, null, null, null]
        

        【讨论】:

          【解决方案4】:

          您可以遍历此数组,并为每一行 filter 取出 null 元素,并收集与 nulls 所在长度相同的剩余 toArray最后:

          String[][] arr1 = {
                  {"O", "X", null, "O", "O", null, null},
                  {null, null, "O", null, null, "O", "O"}};
          
          String[][] arr2 = Arrays.stream(arr1)
                  .map(row -> Arrays.stream(row)
                          // filter out null elements
                          .filter(Objects::nonNull)
                          // new array of the same length, nulls last
                          .toArray(q -> new String[row.length]))
                  .toArray(String[][]::new);
          
          // output
          Arrays.stream(arr2).map(Arrays::toString).forEach(System.out::println);
          
          [O, X, O, O, null, null, null]
          [O, O, O, null, null, null, null]
          

          另见:Remove null from old 2d array and put the not null elements in a new 2d array

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-09
            • 2016-10-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多