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

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

一個接一個地創(chuàng)建新元素

一個接一個地創(chuàng)建新元素

富國滬深 2022-07-14 16:36:55
假設(shè)我的數(shù)組中有現(xiàn)有元素,[10, 20, 30]我希望用戶輸入一個元素,例如。用戶輸入元素4,所以我希望輸出變成這樣[10, 4, 20, 4, 30]。但我只設(shè)法讓用戶輸入元素 10 次(比如我的數(shù)組大?。?,但我只想輸入一次。我不知道如何將“ score.size()”,即大小為 10,只有一個元素,所以我可以得到這樣的輸出[10, 4, 20, 4, 30]。這是我的一些編碼:ArrayList<Double> score = new ArrayList<Double>();// (here I already set some elements in the array)for (int i = 1; i <= score.size(); i += 2){     System.out.println("Enter the element you want to add: ");    double addedElement = in.nextDouble();    score.add(i, addedElement);}System.out.println(score);
查看完整描述

2 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個贊

不要score.size()在循環(huán)中使用,因?yàn)樗拇笮S著您在循環(huán)中添加元素而增加。而是將大小存儲在局部變量中,然后將該變量用于循環(huán)中的條件。


更改如下:


        int n = score.size();

        for (int i = 0; i < n-1; i ++) {


            System.out.println("Enter the element you want to add: ");

            Double addedElement = in.nextDouble();


            score.add(2*i+1, addedElement);

        }

此代碼將要求您為每次迭代提供一個數(shù)字。所以,如果你有一個現(xiàn)有的數(shù)組,[10,20,30]你輸入9和5作為輸入。然后你會得到輸出為[10,9,20,5,30].


如果您只想要一個數(shù)字作為輸入,那么只需將輸入行移到循環(huán)之前。


        int n = score.size();

        System.out.println("Enter the element you want to add: ");

        Double addedElement = in.nextDouble();

        for (int i = 0; i < n-1; i ++) {

            score.add(2*i+1, addedElement);

        }

因此,如果您輸入有一個現(xiàn)有的數(shù)組[10,20,30]并且您輸入9作為輸入。然后你會得到輸出為[10,9,20,9,30].


查看完整回答
反對 回復(fù) 2022-07-14
?
慕田峪4524236

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個贊

首先,這是應(yīng)該執(zhí)行 Seelenvirtuose 所說的代碼。


List<Double> score = new ArrayList<Double>();


//Add the initial stuff

score.add((double)10);

score.add((double)20);

score.add((double)30);


//Get the input from the user

Scanner in = new Scanner(System.in);


System.out.println("Enter the number: ");

double d = in.nextDouble();


in.close();


//Loop through the list and add the input to the correct places

for(int i = 1; i < score.size(); i+= 2)

    score.add(i, d);


System.out.println(score);`

score.size() 返回列表中元素的數(shù)量,因此在您的示例情況下,列表最初包含 10、20 和 30,您的循環(huán)


for (int i = 1; i <= score.size(); i += 2)

System.out.println("Enter the element you want to add: ");

double addedElement = in.nextDouble();


score.add(i, addedElement); 

}

像這樣:

  1. i = 1,score.size() == 3。用戶輸入一個數(shù)字,并將其添加到列表中的 1(10 到 20 之間)。我+= 2。

  2. i == 3,score.size() == 4。用戶輸入另一個數(shù)字,它會轉(zhuǎn)到位置 3(20 到 30 之間)。我+= 2。

  3. i == 5,score.size() == 5。用戶輸入另一個數(shù)字,它會轉(zhuǎn)到位置 5(30 之后)。我+= 2。

  4. i == 7, score.size() == 6。循環(huán)結(jié)束。

更改 score.size() 的方式是添加或刪除元素。在您的示例情況下,它不應(yīng)該達(dá)到 10。希望這有助于理解。

最后,如果您是 Java 新手,請注意數(shù)組(例如 double[])和列表(例如 ArrayList)是非常不同的東西,即使它們用于相似的目的。如果您不知道,您可能想用谷歌搜索他們的差異。


查看完整回答
反對 回復(fù) 2022-07-14
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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