Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suspicious call to java.util.Collection.contains

 
Miran Cvenkel
Ranch Hand
Posts: 202
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j = 0, so these are equal and this should not go into grean line ?
What to do here ?


what does "Suspicious call to java.util.Collection.contains ..." mean ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the warning message, you have a variable nnm_array_list that contains String[][] objects (arrays of arrays of strings). But you are calling the contains() method with a String[] (array of string). The call will always return false, because a String[] is never equal to a String[][] (they are different kinds of objects!).

By the way, most Java programmers don't use a naming style with all lower-case letters and underscores separating words, like: nnm_array_list or all_attributes_matches. The de-facto standard for naming variables in Java is like this: nnmArrayList, allAttributesMatches.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your IDE is probably trying to warn you that you made a programming mistake. It says that the "Given object cannot contain instances of String[] (expected String[][])". That makes me believe that num_array_list is a List<String[][]> and attributes[j] is a String[]. Since all elements of num_array_list will be instances of String[][] they will never be instances of String[] so that call will always be false, the guard to the if-statement is always true and all_attributes_matches will always be set to false.
 
Miran Cvenkel
Ranch Hand
Posts: 202
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I see what ide is warning me about, prog. compiles and runs anyway , what bothers me is what I see in watches part of screen shot.
I see two String[][] , and their contence is equal. And those two are compared in program.
Or m'I wrong here ?
That is, I belive that nnm_array_list contains exactly attributes[0]



The de-facto standard for naming variables in Java is like this: nnmArrayList, allAttributesMatches.

Will try that , doh I find things with underscores much, much easyer to read.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miran Cvenkel wrote:Yes I see what ide is warning me about, prog. compiles and runs anyway , what bothers me is what I see in watches part of screen shot.
I see two String[][] , and their contence is equal. And those two are compared in program.
Or m'I wrong here ?
That is, I belive that nnm_array_list contains exactly attributes[0]

Yes, you are wrong.
Yes, you have two String[][] objects. Yes, they appear to be equal. However, the second String[][] is not attributes[0] but attributes. Therefore, !num_array_list.contains(attributes[0]).

Now you may think that num_array_list.contains(attributes), but the answer cannot be determined. The problem is that List.contains uses the equals method, and for arrays that uses only ==. Therefore, num_array_list.contains(attributes) if and only if there is an element x of num_array_list (e.g. the element that you see) for which x == attributes. In other words, attributes must be the exact same object that is part of your list.
 
Miran Cvenkel
Ranch Hand
Posts: 202
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

However, the second String[][] is not attributes[0] but attributes.

thanks for pointing that out.

I'm getting somewhere, but not there jet (-:


j=0
I guess that now nnm_array_list contains exactly attributes[0]
that is nnm_array_list 0 equals attributes 0

But obviously I'm wrong again ?
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if nnm_array_list.get(0) == attributes[0]. Like I said, array equality uses == only. There are utility methods in class java.util.Arrays (equals and deepEquals) but these are not used in any collection.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic