我有一个链接列表。里面的一切都进展顺利。我唯一的问题是如何将变量与内容进行比较以查看它们是否匹配。
例如,我有一个充满名字的链接列表。我希望用户能够输入名称并搜索以查看该名称是否存在于链接列表中。
用户输入:Johnny
程序检查 Johnny 是否存在于链接列表中。
我没有这方面的代码,因为我不确定它会是什么。
public static LinkedList<String> NameList1 = new LinkedList<String>(); // How do I search its contents?
非常感谢!!
最佳答案
使用List
接口(interface)中的contains()
方法。
if (NameList1.contains("Johnny")) {
// code to execute if Johnny is in the list
}
关于JAVA链表搜索链表将数据与用户输入的数据进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6246176/