如何判断数组中包含某一个值

 时间:2024-10-18 04:35:42

1、四种不同方式检查数组是否包含某个值使用List:public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); }

如何判断数组中包含某一个值

3、使用简单的循环语句:public static boolean useLoop(String[] arr, String targetValue) { for (String s : arr) { if (s.equals(targetValue)) return true; } return false; }

如何判断数组中包含某一个值

5、运行下面程序,你有可能会得到异常结果;public static boolean useArraysBinarySearch(String[] arr, String targetValue) { int a = Arrays.binarySearch(arr, targetValue); if (a > 0) return true; else return false; }

如何判断数组中包含某一个值

7、数组大小为1000:String[] arr = new String[1000]; Random s = new Random(); for (int i = 0; i < 1000; i++) { arr[i] = String.valueOf(s.nextInt()); } 运行结果:useList: 112 useSet: 2055 useLoop: 99 useArrayBinary: 12

如何判断数组中包含某一个值

9、以上就是小编带给大家的如何判断数组中包含某一个值的关键所在,希望大家可以喜欢,如果喜欢的话可以点赞哦,也可以发表自己的看法

  • 怎么判断一个字符串是否包含另一个字符串
  • notepad++设置默认新建格式为文本格式
  • 如何将string转换为int类型
  • 怎么用cmd命令创建文件
  • 用js怎样获得下拉框的值?
  • 热门搜索
    美国西部旅游攻略 春天旅游推荐 旅游团购最好的网站 悦禾旅游网 北京最佳旅游时间 韩国济州岛旅游攻略 张家口天路旅游攻略 浙江旅游胜地 贵州最佳旅游景点 清明 旅游