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

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

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

1.介紹

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

  • 使用線性搜索方法
  • 使用二進(jìn)制搜索方法
  • 使用 List.contains() 方法
  • 使用 Stream.anyMatch() 方法

2.方法

1)使用線性搜索方法

時間復(fù)雜度: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)使用二進(jìn)制搜索方法

通過將搜索間隔重復(fù)分成兩半來搜索排序數(shù)組。從覆蓋整個數(shù)組的區(qū)間開始。如果搜索關(guān)鍵字的值小于區(qū)間中間的項,則將區(qū)間縮小到下半部分。否則,將其縮小到上半部分。反復(fù)檢查,直到找到值或區(qū)間為空。
public static int binarySearch(data_type arr, data_type key)時間復(fù)雜度: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種詳細(xì)方法的文章就介紹到這了,更多相關(guān)Java檢查值是否在數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Springboot封裝好的發(fā)送post請求的工具類

    使用Springboot封裝好的發(fā)送post請求的工具類

    本文介紹了在Springboot中封裝發(fā)送HTTP請求的工具類,并提供了普通的HTTP請求工具類代碼和Response類的使用示例,這些工具類可為開發(fā)者提供便利性和參考價值,幫助提高開發(fā)效率
    2024-09-09
  • Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法示例

    Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法示例

    這篇文章主要介紹了Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法,結(jié)合具體實例形式分析了java的遍歷、遞歸、回溯等算法實現(xiàn)八皇后問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • MybatisPlus如何處理Mysql的json類型

    MybatisPlus如何處理Mysql的json類型

    這篇文章主要介紹了MybatisPlus如何處理Mysql的json類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類

    springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類

    這篇文章主要介紹了springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Springboot項目優(yōu)雅地處理日志的方法詳解

    Springboot項目優(yōu)雅地處理日志的方法詳解

    這篇文章主要介紹了Springboot項目---優(yōu)雅地處理日志,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • Java 注解學(xué)習(xí)筆記

    Java 注解學(xué)習(xí)筆記

    一直都在使用注解,但是一直都沒有用的很明白,后來被逼的發(fā)現(xiàn)不搞明白真的就沒有辦法愉快的寫代碼了,所以,這篇《Java中的注解學(xué)習(xí)筆記》就呼之欲出了
    2020-10-10
  • SpringBoot+JWT實現(xiàn)注冊、登錄、狀態(tài)續(xù)簽流程分析

    SpringBoot+JWT實現(xiàn)注冊、登錄、狀態(tài)續(xù)簽流程分析

    這篇文章主要介紹了SpringBoot+JWT實現(xiàn)注冊、登錄、狀態(tài)續(xù)簽【登錄保持】,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 關(guān)于idea2020.3升級lombok不能使用的問題

    關(guān)于idea2020.3升級lombok不能使用的問題

    這篇文章主要介紹了關(guān)于idea2020.3升級lombok不能使用的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實踐

    SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實踐

    hsqldb只需要添加對應(yīng)的依賴,然后在配置文件進(jìn)行配置。不需要安裝一個數(shù)據(jù)庫,本文就來介紹一下具體使用,感興趣的可以了解一下
    2021-09-09
  • Java?ArrayList遍歷foreach與iterator時remove的區(qū)別

    Java?ArrayList遍歷foreach與iterator時remove的區(qū)別

    這篇文章主要介紹了Java?ArrayList遍歷foreach與iterator時remove的區(qū)別,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07

最新評論