當前位置:首頁 » 編程語言 » java判斷是否在數組中

java判斷是否在數組中

發布時間: 2023-09-25 03:48:01

java中如何高效的判斷數組中是否包含某個元素

可以嫌豎物使用如下的代碼:
Arrays.asList(yourArray).contains(yourValue)
但這並不適用於基本數據類型的數組。
在Java8之後,你可以使用Stream來檢測int,double,long類型的數組是否包含某個數值。(分別用IntStream, DoubleStream和LongStream),例如:
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x ->纖搜 x == 4);
對於數組的一芹液些操作,你可以上秒秒學了解相關的知識。

⑵ 在Java中,如何檢測一個數組中是否包含某一個數據

在Java中,檢測一個數組是否包含某一個數據,通常有四種方法:

(1)for循環

(2)轉換為List,調用Arrays.asList(arr).contains方法

(3)使用Set

(4)使用Arrays.binarySearch()方法

下面為上述四種方法的具體代碼實現:

1、使用for循環

publicstaticbooleanuseLoop(String[]arr,StringtargetValue){

for(Strings:arr){

if(s.equals(targetValue))

returntrue;

}

returnfalse;

}

2、轉換為List,調用Arrays.asList(arr).contains方法

publicstaticbooleanuseList(String[]arr,StringtargetValue){

returnArrays.asList(arr).contains(targetValue);

}

3、使用Set

publicstaticbooleanuseSet(String[]arr,StringtargetValue){

Set<String>老輪搜set=newHashSet<String>(Arrays.asList(arr));

returnset.contains(targetValue);

}

4、使侍歷用Arrays.binarySearch()方法

特別說明:binarySearch()二分查找僅適用於有序數組,如果不是有序數組,則報異常

(String[]arr,StringtargetValue){

inta=Arrays.binarySearch(arr,targetValue);

if(a>0){

returntrue;

}else{

returnfalse;

}}

(2)java判斷是否在數組中擴展閱讀:

Java種List列表的contains方法:

該方法是通過遍歷集合中的每一個元桐猛素並用equals方法比較是否存在指定的元素。

publicbooleancontains(Objecto){

Iterator<E>it=iterator();

if(o==null){

while(it.hasNext())

if(it.next()==null)

returntrue;

}else{

while(it.hasNext())

if(o.equals(it.next()))

returntrue;

}

returnfalse;

}

⑶ java中怎麼判斷一個字元串數組中包含某個字元或字元串

字元串有一個contains方法。

如下:若要查詢字元串數組中是否有「dog"。

如果想查詢字元,只需要將字元串,替換為:字元+「」就可以了。

⑷ 在java中隨機生成10個整數,用戶求輸入一個數,判斷是否存在於這10個整數中

由於你沒有指定這10個整數的生成范圍,所以我這里假定是0~99之間的整數,這樣用戶輸入時有10%的幾率命中。

整體代碼為:

public class Main {

public static void main(String[] args) {
//聲明長度為10的隨機數數組
int[] randoms = new int[10];
Random random = new Random();
for (int i = 0; i < 10; i++) {
//獲取0~99之間的一個隨機整數,可通過敏亮埋調整nextInt的參數來修改隨機數范圍
int num = random.nextInt(100);
//如果新生成的數字已經存在於隨機數數組中,則重新生成
if (checkDistinct(randoms, num)) {
i--;
continue;
}
randoms[i] = num;
}
//增序排序,好看
Arrays.sort(randoms);

System.out.println("請輸入一個整數:");
Scanner scanner = new Scanner(System.in);
//嚴謹一點這里其實可以對輸入的in進行校橋螞驗,檢驗其是不是整數,鍵升校驗方法很多搜一下就有我這就不校驗了
int in = scanner.nextInt();
System.out.println("生成的隨機數數組為:");
System.out.println(Arrays.toString(randoms));
if (checkDistinct(randoms, in)) {
System.out.println("輸入的數字[" + in + "]在其中");
} else {
System.out.println("輸入的數字[" + in + "]不在其中");
}
}

//檢查新生成的數字是否存在於隨機數數組中,若存在,返回true
private static boolean checkDistinct(int[] randoms, int num) {
for (int i = 0; i < randoms.length; i++) {
if (randoms[i] == num) {
return true;
}
}
return false;
}
}

熱點內容
ins海外伺服器ip是什麼 發布:2025-03-06 23:44:59 瀏覽:354
我的世界java版伺服器推薦ip 發布:2025-03-06 23:43:17 瀏覽:562
如何取消微信帳號和密碼 發布:2025-03-06 23:39:20 瀏覽:789
emc存儲備份 發布:2025-03-06 23:35:02 瀏覽:835
蘭州編程培訓 發布:2025-03-06 23:30:51 瀏覽:302
電信託管伺服器阿里雲 發布:2025-03-06 23:27:07 瀏覽:592
艾力紳混動哪個配置值得買 發布:2025-03-06 23:20:30 瀏覽:194
QT靜態編譯64 發布:2025-03-06 23:09:35 瀏覽:283
聖安地列斯自由伺服器ip 發布:2025-03-06 23:09:34 瀏覽:709
安卓怎麼掃二維碼 發布:2025-03-06 23:04:49 瀏覽:515