查找数组中是否存在字符串 [英] Find if a String is present in an array
问题描述
好吧,假设我有一个填充了 {"tube", "are", "fun"} 的数组,然后我有一个 JTextField,如果我输入这些命令中的任何一个来做某事,如果不是像一个消息说找不到命令".
OK let's say I have an array filled with {"tube", "are", "fun"} and then I have a JTextField and if I type either one of those commands to do something and if NOT to get like a message saying "Command not found".
我尝试查看 Java 文档,但我得到的只是一些我不想要的东西,比如问题之类的……那么,这是如何完成的?我知道有一个数组中"函数,但我不太擅长将两者结合在一起.
I tried looking in Java docs but all I am getting is things that I don't want like questions and stuff... so, how is this done? I know there is a "in array" function but I'm not too good with combining the two together.
谢谢.
这是我目前所拥有的:
String[] dan = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};
boolean contains = dan.contains(say.getText());
但我在 dan.contains 中找不到符号
but I am getting cannot find symbol in dan.contains
推荐答案
这就是您要找的:
List<String> dan = Arrays.asList("Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue");
boolean contains = dan.contains(say.getText());
如果您有一个不重复值的列表,最好使用具有相同包含方法
If you have a list of not repeated values, prefer using a Set<String>
which has the same contains method
这篇关于查找数组中是否存在字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!