最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.介紹

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

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

2.方法

1)使用線性搜索方法

時(shí)間復(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ù)組。從覆蓋整個(gè)數(shù)組的區(qū)間開始。如果搜索關(guān)鍵字的值小于區(qū)間中間的項(xiàng),則將區(qū)間縮小到下半部分。否則,將其縮小到上半部分。反復(fù)檢查,直到找到值或區(qū)間為空。
public static int binarySearch(data_type arr, data_type key)時(shí)間復(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)文章

  • Java花式解決'分割回文串 ii'問題詳解

    Java花式解決'分割回文串 ii'問題詳解

    最學(xué)習(xí)動態(tài)規(guī)劃思想的路上,遇見了‘分割回文串問題’,如臨大敵啊,題目聽起來蠻簡單,思考起來卻也沒那么容易,本文將為大家詳細(xì)介紹幾種解決分割回文串 ii問題的辦法,需要的可以參考一下
    2021-12-12
  • 淺談一下Servlet的定義以及運(yùn)行原理

    淺談一下Servlet的定義以及運(yùn)行原理

    相信有很多剛?cè)胄械呐笥褧苫骃ervlet到底是個(gè)什么意思,那么這篇文章就來淺談一下到底什么是Servlet,以及Servlet的原理與如何寫一個(gè)Servlet,,需要的朋友可以參考下
    2023-03-03
  • SpringBoot YAML 配置讀取機(jī)制 + 數(shù)據(jù)庫自動初始化原理解析

    SpringBoot YAML 配置讀取機(jī)制 + 數(shù)據(jù)庫自動初始化原理解析

    本文介紹了SpringBoot中YAML配置的讀取流程,包括配置文件加載、配置綁定和容器管理,還詳細(xì)講解了數(shù)據(jù)庫自動初始化原理,感興趣的朋友跟隨小編一起看看吧
    2025-05-05
  • 一篇文章帶你了解XGBoost算法

    一篇文章帶你了解XGBoost算法

    XGBoost全名叫(eXtreme Gradient Boosting)極端梯度提升,經(jīng)常被用在一些比賽中,其效果顯著。它是大規(guī)模并行boosted tree的工具,它是目前最快最好的開源boosted tree工具包
    2021-08-08
  • Windows系統(tǒng)下JDK版本一鍵、自動切換工具圖文教程

    Windows系統(tǒng)下JDK版本一鍵、自動切換工具圖文教程

    這篇文章主要介紹了Windows系統(tǒng)下JDK版本一鍵、自動切換工具的相關(guān)資料,通過批處理腳本動態(tài)管理環(huán)境變量,支持JDK8-100版本,簡化多項(xiàng)目開發(fā),提升效率并減少配置錯(cuò)誤,需要的朋友可以參考下
    2025-06-06
  • Map按單個(gè)或多個(gè)Value排序當(dāng)Value相同時(shí)按Key排序

    Map按單個(gè)或多個(gè)Value排序當(dāng)Value相同時(shí)按Key排序

    Map可以先按照value進(jìn)行排序,然后按照key進(jìn)行排序。 或者先按照key進(jìn)行排序,然后按照value進(jìn)行排序,這樣操作都行,這篇文章主要介紹了Map按單個(gè)或多個(gè)Value排序,當(dāng)Value相同時(shí)按Key排序,需要的朋友可以參考下
    2023-02-02
  • springboot如何設(shè)置請求參數(shù)長度和文件大小限制

    springboot如何設(shè)置請求參數(shù)長度和文件大小限制

    這篇文章主要介紹了springboot如何設(shè)置請求參數(shù)長度和文件大小限制,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 最新評論

    长阳| 潼关县| 舒兰市| 鄂托克前旗| 石渠县| 磴口县| 从化市| 旺苍县| 弥勒县| 沂水县| 老河口市| 闸北区| 乌鲁木齐县| 宜宾县| 合肥市| 隆林| 清镇市| 高台县| 潼南县| 祁连县| 拉孜县| 娄底市| 七台河市| 红桥区| 克山县| 长兴县| 九台市| 梁平县| 禹州市| 阳城县| 页游| 上饶市| 彭州市| 新宁县| 武安市| 南投市| 漳平市| 天门市| 武鸣县| 日土县| 应城市|