java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java检查值是否在数组

Java中检查值是否存在于数组中的4种详细方法

作者:Smile sea breeze

这篇文章主要给大家介绍了关于Java中检查值是否存在于数组中的4种详细方法,相信大家在操作Java的时候经常会要检查一个数组(无序)是否包含一个特定的值,需要的朋友可以参考下

1.介绍

在 Java 中,有许多方法可以检查此数组中是否存在特定元素。

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检查值是否在数组内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文