5

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;
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Ruben
  • 65
  • 1
  • 1
  • 2
  • 1
    You need to do `store[i] = words[i];`, and then return `store`. – MD Sayem Ahmed Jan 30 '17 at 12:11
  • Possible duplicate of [this](http://stackoverflow.com/questions/9785336/how-to-check-if-array-indexes-are-empty-and-if-so-check-the-next). Please check, this might help you. – Ruchika Sharma Jan 30 '17 at 12:12
  • you are creating store[] new with every iteration. store[] is always an array with words[] length, but holding only emtpy values – baao Jan 30 '17 at 12:14
  • You don't need to recheck your `if` condition in an `else` block. – khelwood Jan 30 '17 at 12:15
  • It seems that what you are aiming to do is replace each word that matches `target` with an empty string, not delete anything. In fact, there are so many things wrong here (you are using `store[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). – RealSkeptic Jan 30 '17 at 12:16

5 Answers5

8

I'm actually not sure what you want to achieve but if you want to remove an empty String out of your array you can do it with streams and filters in java 8 like this:

String[] objects = Arrays.stream(new String[]{"This","", "will", "", "", "work"}).filter(x -> !x.isEmpty()).toArray(String[]::new);
Jérôme
  • 1,254
  • 2
  • 20
  • 25
2

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

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. It just easier to use a 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();
}
Joe ONeil
  • 130
  • 1
  • 6
  • ok i get the logic of it, but can you suggest a method without using (List). Now the problem that im facing is that the initialisation of the copied array. i cant initialise the for loop outside or inside the for loop because i'll only return an array with null values – Ruben Jan 30 '17 at 13:02
  • @Ruben This is the same as the array, just remove the list and use an array instead – AxelH Jan 31 '17 at 08:00
  • If you use an array you need to count the filled elements in the existing array first then use that as the size of the new array or If you use the size of the existing array you will still have empty elements – Joe ONeil Jan 31 '17 at 12:12
  • @Joe ONeil Refer my answer below and do let me know if it helped or if you need further help. – nits.kk Jan 31 '17 at 13:54
1

To check the equality use .equals() method i-e string1.equals(string2) and to check non-equality you can use the same method but with not(!) operator i-e. !string1.equals(string2). You should declare the store array outside the loop because in each iteration it makes a new object onamed store. In the else condition do this store[i] = words[i].

Abdul Malik
  • 306
  • 1
  • 12
0

You shouldn't compare strings using == operator. This is incorrect, because strings are objects. Use .equals() method instead, this should fix your problem.

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]. You should elaborate your code and algorithm.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

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. Default value is null so this is an array of null Strings.
  2. (words[i] != target) should be replaced with

    (!words[i].equals(target))

nits.kk
  • 5,204
  • 4
  • 33
  • 55