java代碼條件查詢數(shù)組,Java數(shù)組查詢

關(guān)于JAVA數(shù)組查詢

返回:

創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元建平做網(wǎng)站,已為上家服務(wù),為建平各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220

如果它包含在數(shù)組的指定范圍內(nèi),則返回搜索鍵的索引;否則返回 (-(插入點(diǎn)) - 1)。插入點(diǎn) 被定義為將鍵插入數(shù)組的那一點(diǎn):如果范圍中的所有元素都小于指定的鍵,則為 toIndex。

以上摘自jdk-api1.6

插入點(diǎn)被定義為toIndex,所以插入點(diǎn)是5,返回值是(-(插入點(diǎn)) - 1)。

也就是-6

用java編寫一個(gè)查詢數(shù)組下標(biāo)的代碼!!

public?static?void?main(String[]?args)?{

Scanner?sca?=?new?Scanner(System.in);

String[]??strs?=?new?String[]{"a",?"b",?"c",?"a",?"b",?"c",?"a",?"b",?"c",?"a",?"b",?"c"};?//定義數(shù)組

String?in?=?sca.next();//鍵盤輸入

String?index?=?"-1,";?//定義下標(biāo)記錄?默認(rèn)-1不存在???,為間隔

for?(int?i?=?0;?i??strs.length;?i++)?{???//循環(huán)查詢下標(biāo)

if?(in.equals(strs[i]))?{??

if?(index.equals("-1,"))?{??//存在則制空?將下標(biāo)放入

index?="";

}

index?+=?i?+?",";?????//循環(huán)放入下標(biāo)

//break;??????????????//如果需求是做單個(gè)的話?可以釋放這段代碼?下面去數(shù)組第一個(gè)值就可以了

}

}

index?=?index.substring(0,?index.length()?-?1);?//取出數(shù)組最后的,

//如果需要下表作為數(shù)字引用??可以這樣

//??String[]?indexs?=?index.split(",");

//??int[]?intIndex?=?new?int[indexs.length];

//??for(int?i?=?0;?i??indexs.length;?i++){

//???intIndex[i]?=?new?Integer(indexs[i]);

//??}

System.out.println(index);

}

這段代碼 多個(gè)單個(gè)都可以 已經(jīng)測(cè)試過了? 只要釋放下代碼就行

如何查詢數(shù)組中是否存在一個(gè)值?并顯示該值所在的位置。用JAVA代碼。

int

a[]={1,2,3,4};

for(int

i

=

0;ia.length;i++){//遍歷數(shù)組查找

if(a[i]

==

2){//找到了就輸出

System.out.println("數(shù)字2是數(shù)組a中第"+(i+1)+“個(gè)元素”);

continue;//找到后就停止循環(huán)

}

}

在Java中,如何檢測(cè)一個(gè)數(shù)組中是否包含某一個(gè)數(shù)據(jù)?

在Java中,檢測(cè)一個(gè)數(shù)組是否包含某一個(gè)數(shù)據(jù),通常有四種方法:

(1)for循環(huán)

(2)轉(zhuǎn)換為List,調(diào)用Arrays.asList(arr).contains方法

(3)使用Set

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

下面為上述四種方法的具體代碼實(shí)現(xiàn):

1、使用for循環(huán)

public static boolean useLoop(String[] arr, String targetValue) {

for (String s : arr) {

if (s.equals(targetValue))

return true;

}

return false;

}

2、轉(zhuǎn)換為List,調(diào)用Arrays.asList(arr).contains方法

public static boolean useList(String[] arr, String targetValue) {

return Arrays.asList(arr).contains(targetValue);

}

3、使用Set

public static boolean useSet(String[] arr, String targetValue) {

SetString set = new HashSetString(Arrays.asList(arr));

return set.contains(targetValue);

}

4、使用Arrays.binarySearch()方法

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

public static boolean useArraysBinarySearch(String[] arr, String targetValue) {

int a = Arrays.binarySearch(arr, targetValue);

if (a 0) {

return true;

} else {

return false;

} }

擴(kuò)展資料:

Java種List列表的contains方法:

該方法是通過遍歷集合中的每一個(gè)元素并用equals方法比較是否存在指定的元素。

public boolean contains(Object o) {

IteratorE it = iterator();

if (o==null) {

while (it.hasNext())

if (it.next()==null)

return true;

} else {

while (it.hasNext())

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

return true;

}

return false;

}

參考資料來源:Java官網(wǎng)-API-Arrays

參考資料來源:Java官網(wǎng)-API-Interface List

名稱欄目:java代碼條件查詢數(shù)組,Java數(shù)組查詢
網(wǎng)址分享:http://www.muchs.cn/article32/phjepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)公司軟件開發(fā)、域名注冊(cè)Google、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)