-1

I'm trying to remove a specific character from an array by copying the non specified elements into a new array. However, when I initialize new array, the return value is giving me a bunch of null values instead of the non specified element array.

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

    }
    return store;
}
Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
Ruben
  • 65
  • 1
  • 1
  • 2
  • 3
    You have to move the initialization of `store (store = new String[words.length];)`out of the loop. – IQV Jan 31 '17 at 07:55
  • This was already stated [in your other question](http://stackoverflow.com/a/41936209/4391450). Why did you need to create this question ? – AxelH Jan 31 '17 at 07:57

3 Answers3

2

You are initializing the result array inside the loop. In other words, for each iteration of the loop, you initialize a new array, and lose the changes you made to the previous one. You should move the initialization outside the loop.

But that would also pose a problem, as you wouldn't be able to preemptively know the size of the resulting array. Java 8 allows you a much easier way to write such a method:

public String[] wordsWithout(String[] words, String target) {
    return Arrays.stream(words)
                 .filter(w -> !w.equals(target))
                 .toArray(String[]::new);
}

EDIT:

As noted in the comments, the design of the OP could be preserved, and the results could be accumulated in a List. IMHO, it's less elegant, but it would work in older versions of Java too:

public String[] wordsWithout(String[] words, String target) {
    List<String> store = new ArrayList<>();
    for (String word : words) {
        if (!word.equals(target)){
            store.add(word);
        }

    }
    return store.toArray(new String(store.size());
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Just an advice, I would split the paragraph about the problem of the array length and `Stream`. They are not related since this would be solve by using a `List` in his code an `toArray` at the end, just like `Stream` – AxelH Jan 31 '17 at 08:07
  • Ok, i dont have java8, is there another method to find out the size of the resulting array. – Ruben Jan 31 '17 at 08:50
0

You are initializing the array each time in for loop. Please check.

Ruchika Sharma
  • 648
  • 1
  • 7
  • 23
0

This is happening because you have of this line: store = new String[words.length]. This means that you are initializing your new Array to an Array of words.length elements all of them being initialized by default with null.

The other problem I see is that you are initializing your Array inside the loop, so at each iteration you will overwrite the changes you have already made in the previous loop (if made any)

To copy the non-specified elements in your new array I will do like @Mureinik mentioned above.

Andrei Olar
  • 2,270
  • 1
  • 15
  • 34