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

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

嘗試打印 ArrayList 的前 4 個元素時出現(xiàn) IndexOutOfBounds

嘗試打印 ArrayList 的前 4 個元素時出現(xiàn) IndexOutOfBounds

元芳怎么了 2021-10-20 14:49:59
...輸出:線程“main”中的異常 java.lang.IndexOutOfBoundsException:在 java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source) at java.base/ 的長度為 1 的索引 1 越界jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source) at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source) at java.base/java.util.Objects.checkIndex(Unknown Source) at java。 base/java.util.ArrayList.get(Unknown Source) at HelloWorld.main(HelloWorld.java:27) ...輸出我的程序是一個待辦事項清單,如下:...Java..................................導(dǎo)入 java .util.Scanner; 導(dǎo)入 java.util.ArrayList;/** * @author Troy * */public class HelloWorld {    /** A very simple to-do list that asks for input and then prints out the  list      * in the form of an ArrayList: eg:     Here is today's to-do list:     [Wake up, Walk the dog,Eat breakfast, Make the bed]      */    public static void main(String[] args) {        // I chose an ArrayList because the size does not have to be predetermined.        ArrayList<String> to_do = new<String>ArrayList();        System.out.println("What would you like to add to your to-do list?");        Scanner user_input = new Scanner(System.in);        //While the user_input still has entries, perform the following:        while (user_input.hasNextLine()) {            //Add next entry in the to-do list(user_input) to the ArrayList            to_do.add(user_input.nextLine());            System.out.println("\n");            /**Print out the ArrayList(to_do) 4 elements at a time. */            for (int i = 0; i < 5; i++ ) {                System.out.println("Your list for today is: " + "\n" + to_do.get(i));            }            }    }}......Java...................................... ……………………一旦我在程序的最后編寫 to_do.get(i) ,我就會收到上述錯誤消息。另一個額外的問題是如何在不影響最終 ArrayList 的輸出的情況下結(jié)束 while 循環(huán)?
查看完整描述

1 回答

?
12345678_0001

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

您的 for 循環(huán)嘗試訪問列表中尚不存在的值。這就是為什么你得到一個IndexOutOfBoundsException.


如果您將 for 循環(huán)中的測試表達式替換為檢查列表大小的內(nèi)容,而不是將其硬編碼為 5,它將起作用:


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

您可以使用break. 在將輸入添加到列表之前檢查輸入,首先將其分配給局部變量。例如,如果它是一個空格(作為用戶沒有任何進一步輸入的信號),則從 while 循環(huán)中執(zhí)行中斷。


String input = user_input.nextLine();

if (" ".equals(input) || i == 4) {

    break;

}

to_do.add(input);

如果你想一次只打印 4 個條目,它看起來像這樣:


System.out.println("Your list for today is: " + "\n");

for (int page = 0; page <= (to_do.size() / 4); page++) {

    for (int offset = 0; offset + page * 4 < to_do.size() && offset < 4; offset++) {

            System.out.println(to_do.get(page * 4 + offset));

        }

        user_input.nextLine();

    }

}


查看完整回答
反對 回復(fù) 2021-10-20
  • 1 回答
  • 0 關(guān)注
  • 271 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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