0

i am trying to compare array list of string with array of string by using .equal but its not working i am trying to remove element from array list if it is equal to arrays element every time it is executing if part and not removing element from array list

    public ArrayList removeCommonWords(ArrayList<String> fileTokens){
    System.err.println("size of arraylist: \t"+fileTokens.size()+"\t size of array: \t"+stoppingWordsGlobal.length);
    for(int i=0;i<fileTokens.size();i++){
        for (int j=0;j<stoppingWordsGlobal.length;j++) {
            if (fileTokens.get(i).equals(stoppingWordsGlobal[j])) {
                fileTokens.remove(i);
                System.out.print("\nremoving token number :"+"\t"+i+"\t"+fileTokens.get(i)+"\t"+stoppingWordsGlobal[j]);
            }
        } 
    }
    return fileTokens;
}
0

2 Answers 2

1

Convert the String[] stringArray to an ArrayList<String>:

ArrayList<String> strings = new ArrayList<>(Arrays.asList(stringArray));

Then simply remove shared content from the original list:

originalList.removeAll(strings);

Or in one line (untested):

originalList.removeAll(new ArrayList<String>(Arrays.asList(stringArray));
Sign up to request clarification or add additional context in comments.

3 Comments

but the same code on the an other program is working fine.. i think its not necessary or it is?
No but it's shorter and easier.
you are checking two arrays are equal or not and i am asking how to remove elements from array list if any element of array is equal to element of array list
0

Try this:

main method code:

ArrayList<String> list= new ArrayList<String>();
      list.add("angularjs");
      list.add("javascript");
      list.add("java");
      list.add("c");

     Test test= new Test();
     System.out.println(test.removeCommonWords(list));

your method:

String[] stoppingWordsGlobal={"nodejs","java","angularjs"};
    public ArrayList removeCommonWords(ArrayList<String> fileTokens){
        System.err.println("size of arraylist: \t"+fileTokens.size()+"\t size of array: \t"+stoppingWordsGlobal.length);
        for(int i=0;i<fileTokens.size();i++){
            for (int j=0;j<stoppingWordsGlobal.length;j++) {
                if (fileTokens.get(i).equals(stoppingWordsGlobal[j])) {
                    String removedString=fileTokens.get(i);
                    fileTokens.remove(i);
                    System.out.print("\nremoving token number :"+"\t"+i+"\t"+removedString+"\t"+stoppingWordsGlobal[j]);
                }
            } 
        }
        return fileTokens;
    }

output is:

size of arraylist: 4 size of array: 3

removing token number : 0 angularjs angularjs

removing token number : 1 java java

2 Comments

i am doing already exactly this but it is not removing i mean if part is working every time either it is equal or not equal.......
It is working fine from my side and returning the arraylist with removed elements. in your method ,you were trying to print the value after removing the element.

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.