• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for an Existing Element in ArrayList of ArrayList  RSS feed

 
Steve Dyke
Ranch Hand
Posts: 1783
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand why the contains method is not working and allowing duplicatate elements.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like fsWORCommentArray only contains ArrayList<String>s, not Strings. Since ArrayList<String> will never equal a String, the contains() should contain false. If you need to find out if the String is in any of the ArrayList<String>s which are in fsWORCommentArray, you will need to loop over fsWORCommentArray, get each ArrayList<String> and check if they contain the String.
 
soundar rajan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are correct, the following code will be printing true:

ArrayList<String> al = new ArrayList<String>();
al.add("hi");
al.add("hsi");



if(al.contains(new String("hi"))){
System.out.println("true");
}else{
System.out.println("false");
}

and the following code will be printing false:

ArrayList<ArrayList<String>> master = new ArrayList<ArrayList<String>>();
ArrayList<String> al = new ArrayList<String>();
al.add("hi");
al.add("hsi");
master.add(al);


if(master.contains(new String("hi"))){
System.out.println("true");
}else{
System.out.println("false");
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!