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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將多個(gè)數(shù)組保存到單個(gè)數(shù)組列表中

如何將多個(gè)數(shù)組保存到單個(gè)數(shù)組列表中

慕村225694 2023-08-04 14:48:36
我試圖將循環(huán)中生成的數(shù)組列表保存到單獨(dú)的數(shù)組列表中。它不允許我這樣做;我收到錯(cuò)誤:public static void ranCentroid() {        Random randomPoint = new Random();        Cent = new ArrayList<>();        for (int i = 0; i < numCen; i++) {            int randomP = randomPoint.nextInt(Points.size());            System.out.println(Points.get(randomP));            Cent.get(i).add(randomP);        }        System.out.println(Cent);    }出現(xiàn)錯(cuò)誤Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0    at java.util.ArrayList.rangeCheck(Unknown Source)    at java.util.ArrayList.get(Unknown Source)    at phase1.Main.ranCentroid(Main.java:100)    at phase1.Main.main(Main.java:41)
查看完整描述

2 回答

?
浮云間

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

我假設(shè) Points 是一個(gè)列表,因?yàn)槟{(diào)用方法Points.size()和Points.get(x)。要總結(jié)您的結(jié)果,請(qǐng)聲明一個(gè)新列表并使用以下方法List.addAll:


public static void ranCentroid() {

    Random randomPoint = new Random();

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

    for (int i = 0; i < numCen; i++) {

        int randomP = randomPoint.nextInt(Points.size());

        result.addAll(Points.get(randomP));            

    }

    System.out.println(result);

}

編輯


如果您需要一個(gè)列表列表作為結(jié)果:


public static void ranCentroid() {

    Random randomPoint = new Random();

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

    for (int i = 0; i < numCen; i++) {

        int randomP = randomPoint.nextInt(Points.size());

        result.add(Points.get(randomP));            

    }

    System.out.println(result);

}


查看完整回答
反對(duì) 回復(fù) 2023-08-04
?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

您要做的是減少功能。縮減函數(shù)接受一個(gè)集合并將其向下一級(jí)縮減。因此,在本例中,將 2D 數(shù)組轉(zhuǎn)換為 1D 數(shù)組。

您也可以在不使用內(nèi)置歸約函數(shù)的情況下通過(guò)創(chuàng)建數(shù)組列表、循環(huán)遍歷元素并將其添加到您創(chuàng)建的數(shù)組列表中來(lái)完成此操作。


查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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