gpt4 book ai didi

java - 将元素添加到对象内的指针数组中

转载 作者:行者123 更新时间:2023-12-01 12:15:38 26 4
gpt4 key购买 nike

class Family
{
String name;
Family[] siblings;
Family mother;
Family father;
Family spouse;
}
public class Pointer
{
public static void main(String args[])
{
Family saun = new Family();
saun.name = "Saun Bladow";

Family shannon = new Family();
shannon.name = "Shannon Bladow";

Family kailey = new Family();
kailey.name = "Kailey Bladow";

Family kyle = new Family();
kyle.name = "Kyle Bladow";
saun.siblings = {shannon, kailey, kyle};
}
}

此代码会导致此错误

illegal start of expression
saun.siblings = {shannon, kailey, kyle};
^

我知道这段代码可以工作

        Family[] siblings = new Family[3];
siblings[0] = shannon;
siblings[1] = kailey;
siblings[2] = kyle;
saun.siblings = siblings;

但考虑到这个家谱有多大,我更愿意使用第一种方法,但我不知道如何让它发挥作用

最佳答案

这取决于语言本身的语法,但在 C# 中你可以这样做。我相信其他语言也有类似的语法,基本上就是创建一个新数组并填充它。

saun.siblings = new Family [] {shannon, kailey, kyle};

关于java - 将元素添加到对象内的指针数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27025521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com