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

后端 未结 5 2175
名媛妹妹
名媛妹妹 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:24

    More code please!

    This works:

    import java.util.*;
    
    public class Contains {
        public static void main(String[] args) {
            List stringList = new ArrayList();
            stringList.add("someString");
            String val = new String("someString");
            if (stringList.contains(val)) {
                System.out.println("The value is in there");
            } else {
                System.out.println("There's no such value here");
            }
        }
    }
    

提交回复
热议问题