【问题标题】:How to remove null values from a 2D array?如何从二维数组中删除空值?
【发布时间】:2012-01-18 09:37:07
【问题描述】:

我在从二维数组中删除空值时遇到了一些问题。我仍然是编程的初学者。我在网上寻找解决方案,但没有发现任何有用的东西。

这是和大学的练习,所以将数组的名称更改为“数组”,对于它的对象也是如此。这就是我所拥有的:

import java.util.ArrayList;

public class Compact
{
    public static void Compact(object[][] array)
    {
        ArrayList<object> list = new ArrayList<object>();

        for(int i=0; i<array.length; i++){
            for(int j=0; j < array[i].length; j++){
                if(array[i][j] != null){
                    list.add(array[i][j]);
                }
            }
        }

        array = list.toArray($not sure what to typ here$);
    }
}

我基于我为一维数组找到的解决方案,但问题是列表是一维的,那么我如何获取二维数组的结构呢? “新”数组必须更小,没有空值。

我想为array[i] 和array[i][j] 分别创建一个列表,但是如何再次将它们合并为 1 个二维数组?

非常感谢所有帮助!

=========================================

编辑:这是解决方案,tnx 大家:

public void compact(Student[][] registrations)
    {
        for(int i=0; i < registrations.length; i++){
            ArrayList<Student> list = new ArrayList<Student>(); // creates a list to store the elements != null
            for(int j = 0; j < registrations[i].length; j++){
                if(registrations[i][j] != null){
                    list.add(registrations[i][j]); // elements != null will be added to the list.
                }
            }
            registrations[i] = list.toArray(new Student[list.size()]); // all elements from list to an array.
        }
    }

【问题讨论】:

    标签: java arrays null 2d


    【解决方案1】:

    Object[][] 对应的可重维结构是List&lt;List&lt;Object&gt;&gt;

    另外,请注意,按照惯例,Java 中的类总是以大写字母开头。因此它应该是Object,而不是object

    二维数组并不是真正的二维数组。它是一个数组数组。因此有一个外部阵列和几个内部阵列。如果你只是想从内部数组中删除空值,你可以在迭代期间只使用一个临时列表来保存当前内部数组的元素。

    for(int i = 0; i < array.length; i++) {
        Object[] inner = array[i];
        List<Object> list = new ArrayList<Object>(inner.length);
        for(int j = 0; j < inner.length; j++){
            if(inner[j] != null){
                list.add(inner[j]);
            }
        }
        array[i] = list.toArray(new Object[list.size()]);
    }
    

    如果外部数组包含空内部数组,并且您还想删除这些,那么您应该使用List&lt;Object[]&gt;

    List<Object[]> outerList = new ArrayList<Object[]>(array.length);
    for(int i = 0; i < array.length; i++) {
        Object[] inner = array[i];
        if (inner != null) {
            List<Object> list = new ArrayList<Object>(inner.length);
            for(int j=0; j < inner.length; j++){
                if(inner[j] != null){
                    list.add(inner[j]);
                }
            }
            outerList.add(list.toArray(new Object[list.size()]));
        }
    }
    array = outerList.toArray(new Object[outerList.size()][]);
    

    【讨论】:

    • 谢谢!我将尝试在练习中实现这一点。有些东西还是有点难以理解,不过我现在可以正确完成了!
    • 我不得不使用 list.toArray 而不是 Arrays.asList。但它仍然没有像第一篇文章中解释的那样工作。
    • 哦,对不起,我会修正答案的。
    • 你的错误是由于你的内部循环,它使用 j,但增加 i 代替:for(int j = 0; j &lt; innerArray.length; i++)。因此,您有一个无限循环,不断创建新数组。
    【解决方案2】:

    只需调整您的逻辑,而不是在最后制作 toArray 使其在第一个数组的末尾。这是代码,s1最后没有空值

        String[][] s = new String[][] { { "00", null, null },
                { "10", "11", null }, { "20", "21", "22" } };
        String[][] s1 = new String[s.length][];
        int k = 0;
    
        for (int i = 0; i < s.length; i++) {
            ArrayList<Object> list = new ArrayList<Object>();
            for (int j = 0; j < s[i].length; j++) {
                if (s[i][j] != null) {
                    list.add(s[i][j]);
                }
            }
            s1[k++] = list.toArray(new String[list.size()]);
        }
    

    【讨论】:

      【解决方案3】:

      对于二维数组中的每一行,您可以将非空值存储在列表中。因此,您将有一个列表列表,其中包含二维数组中的非空值。 现在将其转换回二维数组。

      List<List<Integer> list = new ...  
       for(int i=0; i<array.length; i++){
                     List<Integer> templist = new ... 
                      for(int j=0; j < array[i].length; j++){
                          if(array[i][j] != null){
                              templist .add(array[i][j]);
                          }
                      }
                      if(!templist.isEmpty()){
                         list.add(templist);
                      }
                  }
      ..
      //convert back to 2D array
      
      int[][] arr2d = new int[list.length][];
      for(List<Integer>:list){
       ... poulate the array accordingly
      }
      

      【讨论】:

        猜你喜欢
        • 2014-02-13
        • 1970-01-01
        • 2016-04-25
        • 2021-06-26
        • 2010-12-20
        • 1970-01-01
        • 1970-01-01
        • 2017-09-27
        • 1970-01-01
        相关资源
        最近更新 更多