Java contains doesn't work as expected because “someString” != “someString”

后端 未结 5 2176
名媛妹妹
名媛妹妹 2021-01-18 15:49

I want check whether a String value val is contained within a List of Strings lets call it stringList.

I am doing this

if(stringList.con         


        
5条回答
  •  粉色の甜心
    2021-01-18 16:01

    That doesn’t sound right: contains uses equals rather than ==, so if the string is in the list, it should be found. This can be verified in the indexOf method of the superclass AbstractList used by ArrayList.

    Following your edit, make sure you trim strings before doing contains, as otherwise they may contain the newline character(s).

提交回复
热议问题