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;
}
5
  • 1
    You need to do store[i] = words[i];, and then return store. Commented Jan 30, 2017 at 12:11
  • Possible duplicate of this. Please check, this might help you. Commented Jan 30, 2017 at 12:12
  • you are creating store[] new with every iteration. store[] is always an array with words[] length, but holding only emtpy values Commented Jan 30, 2017 at 12:14
  • You don't need to recheck your if condition in an else block. Commented Jan 30, 2017 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). Commented Jan 30, 2017 at 12:16

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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();
}

4 Comments

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 This is the same as the array, just remove the list and use an array instead
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 Refer my answer below and do let me know if it helped or if you need further help.
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].

Comments

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.

1 Comment

same way ! words[i].equals(target).
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.