我正在尝试通过将现有元素复制到新数组中来删除数组中的空元素。然而,新数组的初始化会导致我的返回值为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;
}
store[i] = words[i];
,然后返回store
。 - MD Sayem Ahmedelse
块中,您不需要重新检查您的if
条件。 - khelwoodstore [i]
,在循环内初始化它,返回原始数组,将字符串作为对象进行比较)。 - RealSkeptic