-1

Trying to remove some words from a LinkedList. This is the data I used for testing:
String[] stopwords = {"the","and"};
nwl = LinkedList<String>(Arrays.asList(input));
String input = "the and of the "
The result I expect to get is: [of] but I am getting: [the, end, of, the].

for (Iterator<String> iter = nwl.iterator(); iter.hasNext();) {
  String word = iter.next();
    for (int i = 0; i < nwl.size(); i++){
      if(word == stopwords[i]) {
        iter.remove();
      }
    }
}
N.N.
  • 15
  • 4
  • if you want to remove duplicate use `Set` if you want to **`iterate`** and remove use `Iterator` but first decide your operation – bananas Dec 29 '16 at 12:13
  • @FlyingZombie Thanks for your comment. I want to keep duplicates, this is why I don't use `Set`. – N.N. Dec 29 '16 at 12:18
  • Although initially this question seems to be about string comparison, in a more careful examination it is also about string splitting/collection initialization. – Itay Maman Dec 29 '16 at 13:41

1 Answers1

1

when you compare strings you need to use the .equals() method and not the == operator. Thus, you need to change if (word == stopwords[i]) to if(word.equals(stopwords[i])).

Longer version:

Roughly speaking, the == operator determines if two variables point at the same object (in our case: whether word and stopwords[i] point at the same string object). the .equals() method determines if two objects are identical (content wise). If your case the program failed to produce the desired output because you had two different strings holding the same content. Thus, comparing them via == yields false whereas comparing them via .equals() yields `true.

EDIT

Having read the program posted in the link, I found a couple of things: First, the inner for loop's condition had to be changed to i < stopwords.length. Second, the newWordList object was not initialized correctly. It was new LinkedList<String>(Arrays.asList(parts)) which means that the LinkedList will contain a single String element whose value is the and of the, which is not what you want. You want the LinkedList to contain four string elements as follows:

  • element 0: the
  • element 1: and
  • element 2: of
  • element 3: the

Thus the initialization need to be changed to new LinkedList<String>( Arrays.asList(parts.split(" "))). Specifically, parts.split(" ") breaks-down the given string (split) into separate words returning an array of these words.

public static void main (String[] args) throws java.lang.Exception
{
    String[] stopwords = { "the", "and" };
    String parts = "the and of the";
    LinkedList<String> newWordList = new LinkedList<String>(
      Arrays.asList(parts.split(" ")));

    for (Iterator<String> iter = newWordList.iterator(); iter.hasNext();) {
        String word = iter.next();
        for (int i = 0; i < stopwords.length; i++) {
            if (word.equals(stopwords[i])) {
                iter.remove();
            }
        }
    }
    System.out.println(newWordList.toString());
}
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
  • Thanks for your answer, made the change you suggested but I am still getting the same output. Maybe there is some other mistake in the `for` loop ? The following link contains the test program [link](https://ideone.com/5tpR3d). – N.N. Dec 29 '16 at 12:56
  • Done replying. please take a look – Itay Maman Dec 29 '16 at 13:12
  • Thank you for your detailed answer and explanation. Working as expected and it also helped me understand a few things. – N.N. Dec 29 '16 at 18:32