在Java中,可以使用List的contains()方法来判断一个字符串是否在列表中。示例代码如下:
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
String str = "apple";
if(list.contains(str)) {
System.out.println(str + " is in the list.");
} else {
System.out.println(str + " is not in the list.");
}
输出结果:
apple is in the list.
如果要判断的字符串不在列表中,则输出结果为:
apple is not in the list.