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

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

字符串中出現(xiàn)的子串的問題

字符串中出現(xiàn)的子串的問題

守候你守候我 2019-03-28 15:19:01
為什么下面的算法不能停止?str 是搜索的字符串, findStr 是我要找的字符串。String str = "helloslkhellodjladfjhello";     String findStr = "hello";     int lastIndex = 0;     int count =0;     while(lastIndex != -1){            lastIndex = str.indexOf(findStr,lastIndex);            if( lastIndex != -1){                  count ++;           }         lastIndex+=findStr.length();     }     System.out.println(count);
查看完整描述

5 回答

?
眼眸繁星

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

lastIndex = str.indexOf(findStr,lastIndex);

返回 findStr 子字符串在 str 字符串中第一次出現(xiàn)處的 索引 ,從指定的 lastIndex 索引開始。因?yàn)閘astIndex=0;所以第一次查找得到的索引還是0。
應(yīng)該把下面的 lastIndex+=findStr.length();放到while循環(huán)中;這樣不會(huì)死循環(huán)了。

查看完整回答
反對 回復(fù) 2019-04-28
?
不負(fù)相思意

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

lastIndex+=findStr.length(); str.indexof() 檢索到字符串返回字符串的位置 因此str中有次字符串 所以lastIndex永遠(yuǎn)不等于-1
所以是個(gè)死循環(huán)

查看完整回答
反對 回復(fù) 2019-04-28
?
POPMUISE

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

你這個(gè)每次進(jìn)去就是在查hello lastIndex永遠(yuǎn)為0嘛


查看完整回答
反對 回復(fù) 2019-04-28
?
揚(yáng)帆大魚

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

lastIndex = str.indexOf(findStr,lastIndex); //確定沒有錯(cuò)誤????


查看完整回答
反對 回復(fù) 2019-04-28
?
www說

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

代碼看起來像這樣。

????String?str?=?"helloslkhellodjladfjhello";
????String?findStr?=?"hello";
????int?lastIndex?=?0;
????int?count?=?0;

????while?(lastIndex?!=?-1)?{
????????lastIndex?=?str.indexOf(findStr,?lastIndex);
????????if?(lastIndex?!=?-1)?{
????????????count++;
????????????lastIndex?+=?findStr.length();
????????}
????}
????System.out.println(count);


查看完整回答
反對 回復(fù) 2019-04-28
  • 5 回答
  • 0 關(guān)注
  • 529 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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