简体   繁体   English

contains()方法不适用于java中的Arrays.asList

[英]contains() method is not working for Arrays.asList in java

I have a string object that looks like: 我有一个字符串对象,看起来像:

String color = "black, pink, blue, yellow";

Now I want to convert it into an array and find a color. 现在我想将它转换为数组并找到一种颜色。 Something like this: 像这样的东西:

boolean check = Arrays.asList(color).contains("pink");

Which always gives false. 这总是假的。

Can anyone help me with this? 谁能帮我这个?

Try this code snippet: 试试这段代码:

boolean check = Arrays.asList("black", "pink", "blue", "yellow").contains("pink");

I wouldn't recommend using String to store multiple values. 我不建议使用String来存储多个值。

Your problem is related to the fact that color is a String not an array so Arrays.asList(color) will create a List which contains only one element that is "black, pink, blue, yellow" that is why it returns false . 你的问题与color是一个String而不是array的事实有关,所以Arrays.asList(color)将创建一个List ,它只包含一个"black, pink, blue, yellow"元素,这就是它返回false

You need first to convert it as an array using split(String regex) as next: 首先需要使用split(String regex)将其转换为array如下所示:

// Here the separator used is a comma followed by a whitespace character
boolean check = Arrays.asList(color.split(",\\s")).contains("pink")

If you only want to know if color contains " pink ", you can also consider using String#contains(CharSequence s) 如果您只想知道color包含“ pink ”,您还可以考虑使用String#contains(CharSequence s)

boolean check = color.contains("pink");

Your string variable color is not an array, so first of all you need to create array from that string variable with split(String dilemeter) method and create ArrayList from splitted string, like this: 您的字符串变量color不是数组,因此首先您需要使用split(String dilemeter)方法从该字符串变量创建数组,并从split(String dilemeter)字符串创建ArrayList ,如下所示:

List<String> arrList = Arrays.asList(color.split(", "));

After that you can check if arrList contains some element: 之后,您可以检查arrList包含一些元素:

boolean check = arrList.contains("pink");

你需要split()字符串

Your color variable is a string. 您的颜色变量是一个字符串。 When you convert to a list it will be inserted as a single string. 转换为列表时,它将作为单个字符串插入。 you can check the output of the following 你可以检查以下输出

Arrays.asList(color).size() Arrays.asList(颜色).size()

The above will always return 1, stating that your understanding that a string with comma's won't be automagically split and converted into a list. 以上将始终返回1,表明您理解带有逗号的字符串不会自动拆分并转换为列表。

you can split at every ' followed by a space as shown below to get your expected output. 您可以在每个'后跟一个空格分割,如下所示,以获得您的预期输出。

System.out.println(Arrays.asList(color.split(", ")).contains("pink")); System.out.println(Arrays.asList(color.split(“,”))。contains(“pink”));

The space is important in the split because your string contains spaces. 空格在拆分中很重要,因为您的字符串包含空格。

split colors to "," , turn that into an arraylist and check if a string is present: 将颜色拆分为“,”,将其转换为arraylist并检查字符串是否存在:

    String color = "black, pink, blue, yellow";
    boolean isThere = Arrays.asList(color.split(",")).contains("black");

    System.out.println("is black present: " + isThere);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM