第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

數(shù)組排列完畢后,如何使用 for 循環(huán)搜索元素?

數(shù)組排列完畢后,如何使用 for 循環(huán)搜索元素?

Smart貓小萌 2023-12-13 10:10:08
按字母順序排列數(shù)組后,我需要添加一個(gè) for 循環(huán)操作來(lái)搜索其中一個(gè)數(shù)組中的特定元素。在這種情況下,我需要搜索“Rogers. Steve”。我應(yīng)該如何進(jìn)行?預(yù)先感謝您的所有幫助!我已經(jīng)嘗試了幾個(gè)例子,但我無(wú)法得到正確的結(jié)果......String[] arrayString=new String[]{ "Administrator", "Vale.Vicky", "Lane.Lois", "Kent.Clark", "Wayne.Bruce", "Parker.Peter", "Rogers.Steve", "Luther.Lex", "Osborn.Harry", "Tony.Stark"};String searchString="Rogers.Steve";//Loop until the length of the array//Print the index of the string on an arraySystem.out.println("Rogers.Steve");我的課。package helloworldjavabook;public class HelloWorldjavabook {  public static void main(String[] args) {    String[] user = {"Administrator", "Vale.Vicky", "Lane.Lois", "Kent.Clark", "Wayne.Bruce",      "Parker.Peter", "Rogers.Steve", "Luther.Lex", " Osborn.Harry", "Tony.Stark"};    orderedGuests(user);    String[] password = {"PASSWORD1", "ILOVEBRUCE", "ILOVECLARK", "  ILOVELOIS", "IAMGOTHAM", "IAMSPECTACULAR",      "IAMAMERICA", "IRUNLEXCORP", "IAMGREENGOBLIN", "  IAMINVINCIBLE"};    orderedGuests(password);    String id[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};    orderedGuests(id);  }  private static void orderedGuests(String[] hotel) {    Arrays.sort(hotel);    System.out.println(Arrays.toString(hotel));  }}
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

public void search() {

    String[] arrayString = new String[]{

      "Administrator",

      "Vale.Vicky",

      "Lane.Lois",

      "Kent.Clark",

      "Wayne.Bruce",

      "Parker.Peter",

      "Rogers.Steve",

      "Luther.Lex",

      "Osborn.Harry",

      "Tony.Stark"};

    String searchString = "Rogers.Steve";


    Arrays.sort(arrayString);

    System.out.println(Arrays.binarySearch(arrayString, searchString));

}

這其中的關(guān)鍵部分是


Arrays.binarySearch(arrayString, searchString)


查看完整回答
反對(duì) 回復(fù) 2023-12-13
?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

我想你正在尋找這樣的東西


String[] arrayString=new String[]{

 "Administrator",

 "Vale.Vicky",

 "Lane.Lois",

 "Kent.Clark",

 "Wayne.Bruce",

 "Parker.Peter",

 "Rogers.Steve",

 "Luther.Lex",

 "Osborn.Harry",

 "Tony.Stark"};

String searchString="Rogers.Steve";


int position = 0;

for (int i = 0; i < arrayString.length; i++) { 

    if(arrayString[i] == searchString ){

        position = i;

    }

}

if( position > 0 ){

    System.out.println("Position: " + position); 

} else {

    System.out.println(searchString + " not found" );

}


查看完整回答
反對(duì) 回復(fù) 2023-12-13
  • 2 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)