欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中檢查值是否存在于數(shù)組中的4種詳細方法

 更新時間:2023年08月24日 11:09:56   作者:Smile?sea?breeze  
這篇文章主要給大家介紹了關(guān)于Java中檢查值是否存在于數(shù)組中的4種詳細方法,相信大家在操作Java的時候經(jīng)常會要檢查一個數(shù)組(無序)是否包含一個特定的值,需要的朋友可以參考下

1.介紹

在 Java 中,有許多方法可以檢查此數(shù)組中是否存在特定元素。

  • 使用線性搜索方法
  • 使用二進制搜索方法
  • 使用 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)使用二進制搜索方法

通過將搜索間隔重復分成兩半來搜索排序數(shù)組。從覆蓋整個數(shù)組的區(qū)間開始。如果搜索關(guān)鍵字的值小于區(qū)間中間的項,則將區(qū)間縮小到下半部分。否則,將其縮小到上半部分。反復檢查,直到找到值或區(qū)間為空。
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 是輸入類型
如果有任何元素,則該函數(shù)返回 true , 否則為假。

public class T1 {
    private static void check(int[] arr, int toCheckValue)
    {
        // 檢查指定元素是否 
        // 是否存在于數(shù)組中 
        // 使用 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

總結(jié)

到此這篇關(guān)于Java中檢查值是否存在于數(shù)組中的4種詳細方法的文章就介紹到這了,更多相關(guān)Java檢查值是否在數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論