java怎么判断字符串在不在list中

28次阅读
没有评论

共计 340 个字符,预计需要花费 1 分钟才能阅读完成。

在 Java 中,可以使用 List 的 contains() 方法来判断一个字符串是否在列表中。示例代码如下:

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

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-21发表,共计340字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)