简体   繁体   English

如何从2D数组中删除空值?

[英]How to remove null values from a 2D array?

I'm having some trouble removing null values from a 2D array. 我在从2D数组中删除空值时遇到一些麻烦。 I'm still a beginner with programming. 我还是编程的初学者。 I looked for solutions on the web, but I didn't find anything useful. 我在网上寻找解决方案,但我找不到任何有用的东西。

This is and exercise at the university, so changed the name of the array just to "array", same for its objects. 这是在大学里练习,所以将数组的名称改为“数组”,对于它的对象也是如此。 This is what I have: 这就是我所拥有的:

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$);
    }
}

I based this on a solution I found for 1D arrays, but the problem is the list is 1D, so how do I get the structure back of the 2D array? 我基于我找到的1D阵列的解决方案,但问题是列表是1D,那么如何获得2D阵列的结构? The "new" array has to be smaller, without the null values. “new”数组必须更小,没有null值。

I thought of making a list for array[i] and one for array[i][j], but then how do I merge them into 1 2D array again? 我想为array [i]制作一个列表,为array [i] [j]制作一个列表,但是如何将它们再次合并为1个2D阵列?

All help is much appreciated! 非常感谢所有帮助!

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

Edit: this is the solution, tnx everyone: 编辑:这是解决方案,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.
        }
    }

The corresponding redimensionable structure to an Object[][] is a List<List<Object>> . Object[][]的相应可重构结构是List<List<Object>>

Also, note that classes in Java always start with an uppercase, by convention. 另请注意,按照惯例,Java中的类始终以大写开头。 It should thus be Object , and not object . 因此它应该是Object ,而不是object

A 2D array is not really a 2D array. 2D阵列实际上不是2D阵列。 It's an array of arrays. 这是一个数组数组。 There is thus one outer array, and several inner arrays. 因此有一个外部阵列和几个内部阵列。 If you just want to remove nulls from the inner arrays, you can just use one temporary list to hold the elements of the current inner array during the iteration. 如果您只想从内部数组中删除空值,则可以在迭代期间使用一个临时列表来保存当前内部数组的元素。

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

If the outer array contains null inner arrays, and you also want to remove these, then you shoud use a List<Object[]> : 如果外部数组包含null内部数组,并且您还想删除它们,那么您应该使用List<Object[]>

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

Just tweak your logic, Instead of making toArray at the end make it at the end of first array. 只需调整你的逻辑,而不是在最后使toArray成为第一个数组的末尾。 Here is the code,s1 have not null values at the end 这是代码,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()]);
    }

For Each Row in the 2D array you can store the not null values in a list. 对于2D数组中的每一行,您可以将非空值存储在列表中。 So you will have a list of list containing not null values from the 2D array. 因此,您将拥有一个包含2D数组中不为空值的列表。 Now convert it back to a 2D array. 现在将其转换回2D数组。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM