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

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

訪問(wèn) ArrayList 中的引用時(shí)出現(xiàn)問(wèn)題

訪問(wèn) ArrayList 中的引用時(shí)出現(xiàn)問(wèn)題

人到中年有點(diǎn)甜 2023-12-13 14:34:47
無(wú)法添加新元素(節(jié)點(diǎn))到ArrayListNode N = new Node(5,"Sandeep");Node N1 = new Node(5,"qwert");在下面的行中我收到空指針異常N.children.add(N1)代碼:class Node {    public int val;    public String data;    public ArrayList<Node> children;    public Node(int val, String data) {        this.val = val;        this.data = data;        ArrayList<Node> children = new ArrayList<Node>();    }}public class Nary {    public static void main(String[] args) {                   // ArrayList<Node> children = new ArrayList<Node>();        Node N = new Node(5,"Sandeep");        Node N1 = new Node(5,"qwert");        N.children.add(N1);    }}
查看完整描述

2 回答

?
holdtom

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

在您的代碼中更改此設(shè)置


class Node {


    public int val;

    public String data;

    public ArrayList<Node> children;


    public Node(int val, String data) {

        this.val = val;

        this.data = data;

        this.children = new ArrayList<Node>();

    }


}


查看完整回答
反對(duì) 回復(fù) 2023-12-13
?
catspeake

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

在 Node 構(gòu)造函數(shù)中,您正在創(chuàng)建 Children 的新本地屬性如果您在構(gòu)造函數(shù)中進(jìn)行以下更改,它將正常工作 this.children = new ArrayList();



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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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