查找数组中是否存在String

查找数组中是否存在String

本文介绍了查找数组中是否存在String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,假设我有一个充满{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());

如果您有不重复值列表,请选择设置< String> 具有相同的方法

If you have a list of not repeated values, prefer using a Set<String> which has the same contains method

这篇关于查找数组中是否存在String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:49