0

lets say my words Array is words={"a","the","in","if","are","it","is"} and my ArrayList contains strings like this {"a table is here", "books are sold","if it is readable"}. i want to remove all the words of array from the arrayList. expected output would be ArrayList as {"table here","books sold","readable"}.

i have tried this so far :

public static void main(String[] args) {
        String[] words = {"a","the","in","if","are","it","is"};
        List<String> wordList = new ArrayList<String>(Arrays.asList(words));
        String[] tArray = {"a table is here", "books are sold","if it is readable"};
        List<String> list = new ArrayList<String>(Arrays.asList(tArray));

        for (int i = 0; i < list.size(); i++) {
            String[] tArrays = list.get(i).split(" ");
            List<String> line = new ArrayList<String>(Arrays.asList(tArrays));
                for (int c = 0; c < wordList.size(); c++) {
                    if (wordList.get(c).equals(line.get(i))) {
                        line.remove(i);
                        i--;
                        break;
                    }
                }//end for
            list.set(i, String.join(" ", line));
        }//end for
        for(String string : list) 
        {
            System.out.println(string);
        }
    }

but not giving the expected output. instead gives an error "java.lang.ArrayIndexOutOfBoundsException: -1"

Jatan Rathod
  • 29
  • 1
  • 5

2 Answers2

1

You should be using line.size() to iterate for all the words after splitting based on " ".

for (int i = 0; i < list.size(); i++) {
        String[] tArrays = list.get(i).split(" ");
        List<String> line = new ArrayList<>(Arrays.asList(tArrays));
        for (int c = 0; c < wordList.size(); c++) {
            if (wordList.get(c).equals(line.get(i))) { // you get i for indexes corresponding to the sentence element and not `line` elements as created above
                line.remove(i); // i is iterated over the list of sentences in your case 3, would start from i=0
                i--; // you change it to '-1' and get IndexOutOfBounds
                break;
            }
        }//end for
        list.set(i, String.join(" ", line));
    }//end for

Though not tested, but you can do something like -

public static void main(String[] args) {
    String[] words = {"a", "the", "in", "if", "are", "it", "is"};
    List<String> wordList = new ArrayList<>(Arrays.asList(words));
    String[] tArray = {"a table is here", "books are sold", "if it is readable"};
    List<String> list = new ArrayList<>(Arrays.asList(tArray));

    for (int i = 0; i < list.size(); i++) {
        String[] tArrays = list.get(i).split(" ");
        List<String> line = new ArrayList<>(Arrays.asList(tArrays));
        for (String lineElement : line) {
            if (wordList.contains(lineElement)) {
                line.remove(lineElement);
            }
        }

        list.set(i, String.join(" ", line));
    }

    for (String string : list) {
        System.out.println(string);
    }
}
Naman
  • 27,789
  • 26
  • 218
  • 353
1

Just to add the java 8 version of doing this kind of filtering:

String[] filtered = 
        list.stream().map(statement -> Arrays.asList(statement.split(" ")))
                     .map(listOfWords -> listOfWords.stream()
                            .filter(word -> !wordList.contains(word))
                            .collect(Collectors.joining(" "))
                         )
                         .toArray(String[]::new);
List<String> filteredList = Arrays.asList(filtered);

You can verify the output using:

filteredList.stream().forEach(System.out::println);

Hope this would be helpful!

STaefi
  • 4,297
  • 1
  • 25
  • 43