Java中檢查值是否存在于數(shù)組中的4種詳細方法
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)文章
使用Springboot封裝好的發(fā)送post請求的工具類
本文介紹了在Springboot中封裝發(fā)送HTTP請求的工具類,并提供了普通的HTTP請求工具類代碼和Response類的使用示例,這些工具類可為開發(fā)者提供便利性和參考價值,幫助提高開發(fā)效率2024-09-09Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法示例
這篇文章主要介紹了Java基于循環(huán)遞歸回溯實現(xiàn)八皇后問題算法,結(jié)合具體實例形式分析了java的遍歷、遞歸、回溯等算法實現(xiàn)八皇后問題的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-06-06springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類
這篇文章主要介紹了springboot如何通過不同的策略動態(tài)調(diào)用不同的實現(xiàn)類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot+JWT實現(xiàn)注冊、登錄、狀態(tài)續(xù)簽流程分析
這篇文章主要介紹了SpringBoot+JWT實現(xiàn)注冊、登錄、狀態(tài)續(xù)簽【登錄保持】,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06關(guān)于idea2020.3升級lombok不能使用的問題
這篇文章主要介紹了關(guān)于idea2020.3升級lombok不能使用的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12SpringBoot集成內(nèi)存數(shù)據(jù)庫hsqldb的實踐
hsqldb只需要添加對應的依賴,然后在配置文件進行配置。不需要安裝一個數(shù)據(jù)庫,本文就來介紹一下具體使用,感興趣的可以了解一下2021-09-09Java?ArrayList遍歷foreach與iterator時remove的區(qū)別
這篇文章主要介紹了Java?ArrayList遍歷foreach與iterator時remove的區(qū)別,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07