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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這個 while 循環(huán)不會中斷?

為什么這個 while 循環(huán)不會中斷?

慕桂英3389331 2023-07-28 15:50:45
這是用 Java 編寫的 while 循環(huán)。目的是從列表中刪除所有重復(fù)項。但循環(huán)并沒有中斷??赡苓€有其他錯誤。public static <E extends Comparable<? super E>> void removeDuplicates(ArrayList<E> L) {    ArrayList<E> temp = new ArrayList<E>(L.size());    ArrayList<Integer> index = new ArrayList<>(L.size());    int stop = 0;    while (true) {        //test for duplicates and save their indexes        for (int i = 0; i < L.size(); i++) {            if (!temp.contains(L.get(i))) {                temp.add(L.get(i));                index.add(i);            }        }        // if there were duplicates they will be removed        if (!index.isEmpty()) {            stop = 1;            for (int j = 0; j < index.size(); j++) {                L.remove(index.get(j));            }        }        //if nothing is removed there should be no duplicates and the loop should break        if (stop == 0) {            break;        }        index.clear();        temp.clear();        stop = 0;    }} 
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經(jīng)驗 獲得超6個贊

當(dāng)臨時列表中已存在要刪除的項目時,您需要更新它們的列表。因此索引列表將包含所有重復(fù)元素的索引:


public static <E extends Comparable<? super E>> void removeDuplicates(final ArrayList<E> L) {

final ArrayList<E> temp = new ArrayList<E>(L.size());

final ArrayList<Integer> index = new ArrayList<>(L.size());

int stop = 0;


while (true) {

  //test for duplicates and save their indexes

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

    if (!temp.contains(L.get(i))) {

      temp.add(L.get(i));

    } else {


      index.add(i);

    }

  }

  // if there were duplicates they will be removed

  if (!index.isEmpty()) {

    stop = 1;

    for (int j = index.size() - 1; j >= 0; j--) {

      L.remove((int) index.get(j));

    }

  }

  //if nothing is removed there should be no duplicates and the loop should break

  if (stop == 0) {

    break;

  }

  index.clear();

  temp.clear();

  stop = 0;

}


查看完整回答
反對 回復(fù) 2023-07-28
  • 1 回答
  • 0 關(guān)注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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