留言与评论(共有 0 条评论) |
发布时间:2020-04-12 09:14:35
在Java中,检测一个数组是否包含某一个数据,通常有四种方法:(1)for循环(2)转换为List,调用Arrays.asList(arr).contains方法(3)使用Set(4)使用Arrays.binarySearch()方法下面为上述四种方法的具体代码实现:1、使用for循环public static boolean useLoop(String[] arr, String targetValue) {for (String s : arr) {if (s.equals(targetValue))return true;}return false; }2、转换为List,调用Arrays.asList(arr).contains方法public static boolean useList(String[] arr, String targetValue) {return Arrays.asList(arr).contains(targetValue);}public static boolean useSet(String[] arr, String targetValue) {Set
留言与评论(共有 0 条评论) |
全站搜索