Java中如何檢查數(shù)組是否包含某整數(shù)
在 Java 中檢查數(shù)組是否包含某整數(shù)
本教程介紹了如何在 Java 中檢查一個數(shù)組是否包含整數(shù)值,并列舉了一些示例代碼來理解這個主題。
數(shù)組是一個存儲相同數(shù)據(jù)類型元素的容器。例如,一個整數(shù)組只能有整數(shù)類型的值。在這里,我們將檢查一個數(shù)組是否包含給定的指定值。在本文中,我們使用了幾個內(nèi)置的方法,如 anyMatch()
、contains()
、binarySearch()
等,我們將在給定的數(shù)組中找到一個值。
使用 anyMatch()方法檢查數(shù)組是否包含指定的值
我們可以使用 anyMatch()
方法在給定的數(shù)組中找到指定的值。這個方法返回一個布爾值,要么是 true
,要么是 false
,它需要一個 lambda 表達式作為參數(shù),可以在 Java 8 或更高版本中使用。
import java.util.Arrays; public class SimpleTesting{ public static void main(String[] args) { int[] arr = {10,25,23,14,85,65}; int key = 14; boolean val = contains(arr, key); System.out.println("Array contains "+key+"? \n"+val); } public static boolean contains(final int[] arr, final int key) { return Arrays.stream(arr).anyMatch(i -> i == key); } }
輸出:
Array contains 14?
true
使用 contains() 方法檢查數(shù)組中是否包含指定的值
我們可以使用 contains()
方法在給定數(shù)組中找到指定的值。這個方法返回一個布爾值,要么是 true
,要么是 false
。它需要兩個參數(shù),第一個是數(shù)組,第二個是要查找的值。contains()
方法屬于 Apache commons 庫的 ArrayUtils
類。請看下面的例子。
import org.apache.commons.lang3.ArrayUtils; public class SimpleTesting{ public static void main(String[] args) { int[] arr = {10,25,23,14,85,65}; int key = 14; boolean val = contains(arr, key); System.out.println("Array contains "+key+"? \n"+val); } public static boolean contains(final int[] arr, final int key) { return ArrayUtils.contains(arr, key); } }
輸出:
Array contains 14?
true
使用列表的 contains() 方法檢查一個數(shù)組是否包含指定的值
我們可以通過 Arrays.asList()
將數(shù)組轉(zhuǎn)換為列表,然后使用列表的 contains()
方法在給定數(shù)組中找到指定的值。這個方法返回一個布爾值,可以是 true
或 false
。它以一個需要被找到的值作為參數(shù)。請看下面的例子。
import java.util.Arrays; public class SimpleTesting{ public static void main(String[] args) { int[] arr = {10,25,23,14,85,65}; int key = 14; boolean val = contains(arr, key); System.out.println("Array contains "+key+"? \n"+val); } public static boolean contains(final int[] arr, final int key) { return Arrays.asList(arr).contains(key); } }
輸出:
Array contains 14?
true
使用 binarySearch() 方法檢查一個數(shù)組是否包含指定的值
我們可以使用 binarySearch()
方法在給定的數(shù)組中找到指定的值。這個方法在匹配后返回一個值。如果數(shù)組是已經(jīng)排序的,那么它就會工作,所以在應用這個方法之前,先對數(shù)組進行排序。請看下面的例子。
import java.util.Arrays; public class SimpleTesting{ public static void main(String[] args) { int[] arr = {10,25,23,14,85,65}; int key = 14; boolean val = contains(arr, key); System.out.println("Array contains "+key+"? \n"+val); } public static boolean contains(final int[] arr, final int key) { Arrays.sort(arr); return Arrays.binarySearch(arr, key) >= 0; } }
輸出:
Array contains 14?
true
使用自定義代碼檢查數(shù)組是否包含指定的值
我們可以使用自定義代碼在給定的數(shù)組中找到指定的值。我們創(chuàng)建一個自定義方法來查找數(shù)組中的值,并返回一個布爾值,要么是 true
,要么是 false
。這個方法需要兩個參數(shù),第一個是數(shù)組,第二個是需要找到的值。請看下面的例子。
public class SimpleTesting{ public static void main(String[] args) { int[] arr = {10,25,23,14,85,65}; int key = 14; boolean val = contains(arr, key); System.out.println("Array contains "+key+"? \n"+val); } public static boolean contains(final int[] arr, final int key) { boolean found = false; for(int i = 0; i < arr.length; i++) { if(arr[i]==key) { found=true; } } return found; } }
輸出:
Array contains 14?
true
Java之判斷數(shù)組中是否包含某個值
方式一:使用Arrays.asList(str).contains()
public static boolean useList(String[] arr, String targetValue) { return Arrays.asList(arr).contains(targetValue); }
示例如下:
String[] str={"學歷教育","專業(yè)教育","通識教育","其它在職訓"} ; if (!Arrays.asList(str).contains(excels.get(i).getTrainingType())){ return new BaseResult<>(BaseErrMsg.DB_INSERT_FAILURE,"第"+(i+1)+"行教育訓練類別有誤,請重新輸入!"); }
方式二:使用set.contains()
public static boolean useSet(String[] arr, String targetValue) { Set<String> set = new HashSet<String>(Arrays.asList(arr)); return set.contains(targetValue); }
方式三:使用for循環(huán)
public static boolean useLoop(String[] arr, String targetValue) { for (String s : arr) { if (s.equals(targetValue)) return true; } return false; }
到此這篇關于在 Java 中檢查數(shù)組是否包含某整數(shù)的文章就介紹到這了,更多相關java檢查數(shù)組是否包含某整數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
idea install 時提示jdk的某個jar包的包不存在的問題
這篇文章主要介紹了idea install 時提示jdk的某個jar包的包不存在的問題,本文給大家分享解決方法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例
這篇文章主要介紹了Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例的相關資料,需要的朋友可以參考下2017-07-07