How to check for a element in LIst in goovy
I have:
List list = [3,5,6]
now i want to check whether the List has elemen开发者_运维知识库t "3" .when i do:
list.contains("3")
it returns false ,so how to do this
As others have mentioned the problem with your code is that the list contains the Integer 3, not the String "3". To check for the Integer 3, use either:
[1, 2, 3].contains(3)
or
3 in [1, 2, 3]
Although I do not know groovy I think that you should try list.contains(3)
, i.e. value without quotes. It is because you actually created list of integer elements and then trying to look for string into the list.
I don't know groovy that much, but your list seems to contain integers and you check for a string. Try list.contains(3)
.
精彩评论