Java中检查值是否存在于数组中的4种详细方法
1.介绍
在 Java 中,有许多方法可以检查此数组中是否存在特定元素。
- 使用线性搜索方法
- 使用二进制搜索方法
- 使用 List.contains() 方法
- 使用 Stream.anyMatch() 方法
2.方法
1)使用线性搜索方法
时间复杂度:O(N) 辅助空间:O(1)
for (int element : arr) {
if (element == toCheckValue) {
return true;
}
}public class T1 {
private static void check(int[] arr, int toCheckValue)
{
boolean test = false;
for (int element : arr) {
if (element == toCheckValue) {
test = true;
break;
}
}
System.out.println("Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args) {
int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
int toCheckValue = 7;
System.out.println("Array: "+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
2)使用二进制搜索方法
通过将搜索间隔重复分成两半来搜索排序数组。从覆盖整个数组的区间开始。如果搜索关键字的值小于区间中间的项,则将区间缩小到下半部分。否则,将其缩小到上半部分。反复检查,直到找到值或区间为空。
public static int binarySearch(data_type arr, data_type key)时间复杂度:O(nlog(n)) 辅助空间:O(1)
public class T1 {
private static void check(int[] arr, int toCheckValue) {
Arrays.sort(arr);
int res = Arrays.binarySearch(arr, toCheckValue);
boolean test = res >= 0 ? true : false;
System.out.println("Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args) {
int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
int toCheckValue = 7;
System.out.println("Array: "+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
3)使用 List.contains() 方法
Java 中的 List contains() 方法用于检查指定元素是否存在于给定列表中。
public boolean contains(Object)
public class T1 {
private static void check(Integer[] arr, int toCheckValue) {
boolean test= Arrays.asList(arr) .contains(toCheckValue);
System.out.println("Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args) {
Integer arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
int toCheckValue = 7;
System.out.println("Array: "+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
4)使用 Stream.anyMatch() 方法
boolean anyMatch(Predicate<T> predicate)T 是输入类型
如果有任何元素,则该函数返回 true , 否则为假。
public class T1 {
private static void check(int[] arr, int toCheckValue)
{
// 检查指定元素是否
// 是否存在于数组中
// 使用 anyMatch() 方法
boolean test
= IntStream.of(arr)
.anyMatch(x -> x == toCheckValue);
System.out.println("Is " + toCheckValue
+ " present in the array: " + test);
}
public static void main(String[] args) {
int arr[] = { 5, 1, 1, 9, 7, 2, 6, 10 };
int toCheckValue = 7;
System.out.println("Array: "+ Arrays.toString(arr));
check(arr, toCheckValue);
}
}Array: [5, 1, 1, 9, 7, 2, 6, 10]
Is 7 present in the array: true
总结
到此这篇关于Java中检查值是否存在于数组中的4种详细方法的文章就介绍到这了,更多相关Java检查值是否在数组内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
java 8 lambda表达式list操作分组、过滤、求和、最值、排序、去重代码详解
java8的lambda表达式提供了一些方便list操作的方法,主要涵盖分组、过滤、求和、最值、排序、去重,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧2024-01-01
SpringBoot YAML 配置读取机制 + 数据库自动初始化原理解析
本文介绍了SpringBoot中YAML配置的读取流程,包括配置文件加载、配置绑定和容器管理,还详细讲解了数据库自动初始化原理,感兴趣的朋友跟随小编一起看看吧2025-05-05
Map按单个或多个Value排序当Value相同时按Key排序
Map可以先按照value进行排序,然后按照key进行排序。 或者先按照key进行排序,然后按照value进行排序,这样操作都行,这篇文章主要介绍了Map按单个或多个Value排序,当Value相同时按Key排序,需要的朋友可以参考下2023-02-02


最新评论