在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.