亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

檢查布爾數組中所有值的快速方法

檢查布爾數組中所有值的快速方法

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

1 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超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;

}

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


查看完整回答
反對 回復 2023-07-28
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號