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.
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];, and then returnstore.ifcondition in anelseblock.targetwith an empty string, not delete anything. In fact, there are so many things wrong here (you are usingstore[i]without ever assigning it a value, you're initializing it within the loop, you are returning your original array, you compare strings as objects).