简体   繁体   English

从数组中删除空元素(Java)

[英]Removing empty element from Array(Java)

I'm trying to remove the empty element from the array by copying the existing element to a new array. 我试图通过将现有元素复制到新数组来从数组中删除空元素。 However, initialization of the new array is causing my return value to be null even when I initialize it within the for loop. 但是,新数组的初始化导致我的返回值为null,即使我在for循环中初始化它也是如此。

public String[] wordsWithout(String[] words, String target) {
    for(int i = 0; i < words.length; i = i +1){
        String store[] = new String[words.length];
        if (words[i] == target){
            words[i] ="";
        }
        else if(words[i] != target){
            words[i] = store[i];
        }       
    }
    return words;
}

我实际上不确定你想要实现什么,但是如果你想从数组中删除一个空字符串,你可以使用java 8中的流和过滤器来完成它,如下所示:

String[] objects = Arrays.stream(new String[]{"This","", "will", "", "", "work"}).filter(x -> !x.isEmpty()).toArray(String[]::new);

Array are immutable so the size stays the same you need to create a new Array So if you create a new Array base on the Size of the Old array you will still have null elements 数组是不可变的,所以大小与创建新数组所需的大小相同所以如果你在Old数组的大小上创建一个新的数组,你仍然会有null元素

If you want to use arrays only you need to count the non null elements in the array to get the size of the new Array. 如果只想使用数组,则需要计算数组中的非null元素以获取新数组的大小。 It just easier to use a List/ArrayList 它更容易使用List / ArrayList

public String[] wordsWithout(String[] words, String target) {
    List<String> tempList=new ArrayList<String>();
    for(int i = 0; i < words.length; i = i +1){

        if (words[i]!=null||words[i].trim().length()>0){
            tempList.add(words[i]);
        }

    }
    return (String[]) tempList.toArray();
}

To check the equality use .equals() method ie string1.equals(string2) and to check non-equality you can use the same method but with not(!) operator ie. 要检查相等性,请使用.equals()方法,即string1.equals(string2)并检查不相等,您可以使用相同的方法,但不使用(!)运算符即。 !string1.equals(string2). !string1.equals(字符串2)。 You should declare the store array outside the loop because in each iteration it makes a new object onamed store. 您应该在循环外部声明存储数组,因为在每次迭代中它都会创建一个新对象的onamed存储。 In the else condition do this store[i] = words[i]. 在else条件下,这个存储[i] = words [i]。

You shouldn't compare strings using == operator. 您不应该使用==运算符比较字符串。 This is incorrect, because strings are objects. 这是不正确的,因为字符串是对象。 Use .equals() method instead, this should fix your problem. 使用.equals()方法,这应该可以解决您的问题。

Rest part of your code is quite confusing, it's hard to understand what you are trying to achieve: you create new string array store each time in loop iterations, and then assign its null (by default) values to words[i] . 您的代码的其余部分非常混乱,您很难理解您要实现的目标:每次在循环迭代中创建新的字符串数组store ,然后将其null (默认情况下)值分配给words[i] You should elaborate your code and algorithm. 你应该详细说明你的代码和算法。

There are few things Which I am putting as points below. 我在下面列出的内容很少。 Hope you will get help from this. 希望你能得到这方面的帮助。

  1. String store[] = new String[words.length] instantiates an array of Strings but It does not instantiate any of the elements with any non null value. String store[] = new String[words.length]实例化一个字符串数组,但它没有实例化任何具有任何非空值的元素。 Default value is null so this is an array of null Strings. 默认值为null,因此这是一个null字符串数组。
  2. (words[i] != target) should be replaced with (words[i] != target)应替换为

    (!words[i].equals(target)) (!字[I] .equals(目标))

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

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