返回:

創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站設計、成都網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元建平做網(wǎng)站,已為上家服務,為建平各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
如果它包含在數(shù)組的指定范圍內(nèi),則返回搜索鍵的索引;否則返回 (-(插入點) - 1)。插入點 被定義為將鍵插入數(shù)組的那一點:如果范圍中的所有元素都小于指定的鍵,則為 toIndex。
以上摘自jdk-api1.6
插入點被定義為toIndex,所以插入點是5,返回值是(-(插入點) - 1)。
也就是-6
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,";?//定義下標記錄?默認-1不存在???,為間隔
for?(int?i?=?0;?i??strs.length;?i++)?{???//循環(huán)查詢下標
if?(in.equals(strs[i]))?{??
if?(index.equals("-1,"))?{??//存在則制空?將下標放入
index?="";
}
index?+=?i?+?",";?????//循環(huán)放入下標
//break;??????????????//如果需求是做單個的話?可以釋放這段代碼?下面去數(shù)組第一個值就可以了
}
}
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);
}
這段代碼 多個單個都可以 已經(jīng)測試過了? 只要釋放下代碼就行
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)+“個元素”);
continue;//找到后就停止循環(huán)
}
}
在Java中,檢測一個數(shù)組是否包含某一個數(shù)據(jù),通常有四種方法:
(1)for循環(huán)
(2)轉換為List,調(diào)用Arrays.asList(arr).contains方法
(3)使用Set
(4)使用Arrays.binarySearch()方法
下面為上述四種方法的具體代碼實現(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、轉換為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ù)組,則報異常
public static boolean useArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if (a 0) {
return true;
} else {
return false;
} }
擴展資料:
Java種List列表的contains方法:
該方法是通過遍歷集合中的每一個元素并用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.jbt999.com/article32/phjepc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、網(wǎng)站設計公司、軟件開發(fā)、域名注冊、Google、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)