contains()方法不适用于Java中的Arrays.asList [英] contains() method is not working for Arrays.asList in java

查看:604
本文介绍了contains()方法不适用于Java中的Arrays.asList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的字符串对象:

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");

总是给出假的.

有人可以帮我吗?

推荐答案

您的字符串变量color不是数组,因此首先您需要使用split(String dilemeter)方法从该字符串变量创建数组并创建来自拆分的字符串,如下所示:

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:

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

之后,您可以检查arrList是否包含某些元素:

After that you can check if arrList contains some element:

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

这篇关于contains()方法不适用于Java中的Arrays.asList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆