File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Suspicious call to java.util.Collection.contains Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Suspicious call to java.util.Collection.contains" Watch "Suspicious call to java.util.Collection.contains" New topic
Author

Suspicious call to java.util.Collection.contains

Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147
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 ?


Searchable nature photo gallery: http://agrozoo.net/jsp/Galery.jsp?l2=en
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Nov 23, 2010
Posts: 147

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Suspicious call to java.util.Collection.contains