我试图通过将现有元素复制到新数组中删除空元素。但是,即使我在for
循环内初始化它,新数组的初始化也会导致我的返回值为空。从数组(Java)中删除空元素
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;
}
你需要'store [i] = words [i];',然后返回'store'。 –
[this]的可能重复(http://stackoverflow.com/questions/9785336/how-to-check-if-array-indexes-are-empty-and-if-so-check-the-next)。请检查,这可能会帮助你。 –
您正在为每次迭代创建store []。 store []总是一个长度为[]长度的数组,但只保存非空值 – baao