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