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

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

檢查布爾數(shù)組中所有值的快速方法

檢查布爾數(shù)組中所有值的快速方法

海綿寶寶撒 2023-07-28 16:03:50
我剛剛開始使用 java,在做網(wǎng)絡(luò)練習(xí)時,我創(chuàng)建了一個長度為“n”的布爾數(shù)組。之后,我決定創(chuàng)建一個 while 循環(huán),當(dāng)布爾數(shù)組的所有條目都為 true 時停止。有沒有辦法快速做到這一點?我想到的唯一方法是放置“while(!(array[0] && array[1]... && array[n]),但這需要大量工作。有什么想法嗎?
查看完整描述

1 回答

?
德瑪西亞99

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

只需瀏覽整個列表并檢查真假


public static void main(String[] args) {

    boolean[] array = new boolean[] {false,true,true,false,true};

    int index = 0;

    while(!checkBooleanArray(array)) { //check

        array[index] = true;           //do something about it

        index++;                       //increasing the index and then check again

    }                                  //you obviously have to change the the part of "do something" and "increasing index" to match your wishes

    System.out.println(Arrays.toString(array));

}


public static boolean checkBooleanArray(boolean[] array) {

    for (boolean b : array)

        if (!b)

            return false;

    return true;

}

另一種更快的方法是在更改數(shù)組的一個布爾值時嘗試對數(shù)組進(jìn)行排序,然后僅檢查數(shù)組中的第一個或最后一個布爾值


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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