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

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

有沒有一種算法可以圍繞 ArrayList 進(jìn)行循環(huán)迭代?

有沒有一種算法可以圍繞 ArrayList 進(jìn)行循環(huán)迭代?

拉風(fēng)的咖菲貓 2023-10-13 09:58:32
也就是說,當(dāng)我到達(dá)紙張末尾時(shí),下一個(gè)元素為零。
查看完整描述

3 回答

?
縹緲止盈

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

嘗試這個(gè):


ArrayList<Object> list = new ArrayList<>(); // + add some values to the list


for (int i = 0; i < list.size(); i++) {

    someMethod();

    if (some condition) {

        break; // you need to add some break condition, otherwise, this will be an infinite loop

    }

    if (i == list.size() - 1) {

        i = -1;

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
開滿天機(jī)

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

就在這里:


考慮以下代碼:


for (int i = 0; i < 100; i++) {

   // Output will be: 0,1,2,3,4,5,6,7;0,1,2,3,4,5,6,7;...

   System.out.println(i % 8);

}


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
萬千封印

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

鑒于您已經(jīng)聲明并填充了ArrayList我將調(diào)用的list,那么您只需對(duì)列表大小取模即可進(jìn)行迭代。具體如何寫取決于您想要做什么。


1)一直循環(huán)下去:


int index = 0;

while (true) {

    value = list.get(index);

    … process value here …

    index = (index + 1) % list.size();

    // or equivalently to previous line: if (++index >= list.size) index = 0;

}

2) 精確地循環(huán)列表一次,但從某個(gè)任意點(diǎn)開始base:


for (int offset = 0; offset < list.size(); offset++) {

    int index = (base + offset) % list.size();

    value = list.get(index);

    … process value here …

}

等等...


可以設(shè)計(jì)方法來使用顯式迭代器而不是索引,但這完全取決于您想要實(shí)現(xiàn)的目標(biāo)。


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

添加回答

舉報(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)