Android - 检查数组中是否存在值 [英] Android - Check whether a value exist in an Array

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

问题描述

我有一个名为bob的数组,其中包含值。

I have an array named "bob" which contains values.

String[] bob = { "this", "is", "a", "really", "silly", "list" };

如何在不迭代的情况下知道名为bob的数组中是否存在愚蠢值?

How can I know whether "silly" value exists in the array named bob without iterating through it?

推荐答案

您可以使用 List#contains 方法。为此,您需要将数组转换为列表。您可以使用 数组#asList() 方法如下:

You can use List#contains method. For that you need to convert your array to a list. You can use Arrays#asList() method fot that:

String[] bob = { "this", "is", "a", "really", "silly", "list" };

if (Arrays.asList(bob).contains("silly")) {
    // true
}

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

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