如何在main中初始化一个对象数组?

Posted

技术标签:

【中文标题】如何在main中初始化一个对象数组?【英文标题】:How to initialise an array of object in main? 【发布时间】:2020-07-01 18:59:39 【问题描述】:

我有一个人员对象数组的构造函数:

public class Group

    final int MAX =10;
    private Person[] _groupOfPersons;
    private int _numOfPersons;


    public Group()
    
        _groupOfPersons = new Person [MAX];
        _numOfPersons = 0;
    

我也有 addPerson 方法

如何在 main 中声明新数组?所以我可以在人之后添加?我得到空异常。 我想调用数组'group1'。数组是 Person 类型吗?还是集团?如何创建新组?

Group[] group1 = new Group() \\????

【问题讨论】:

【参考方案1】:

方法真的很多:

public static void main(String[] args) 
    class Person 
        private String name;

        public Person(String name) 
            this.name = name;
        

        @Override
        public String toString() 
            return "Person" +
                    "name='" + name + '\'' +
                    '';
        
    

    class Group 
        Person[] groupOfPerson = new Person[10];

        public Group() 
        

        public Group(Person[] groupOfPerson) 
            this.groupOfPerson = groupOfPerson;
        

        public Person[] getGroupOfPerson() 
            return groupOfPerson;
        

        public void setGroupOfPerson(Person[] groupOfPerson) 
            this.groupOfPerson = groupOfPerson;
        

        @Override
        public String toString() 
            return "Group" +
                    "groupOfPerson=" + Arrays.toString(groupOfPerson) +
                    '';
        
    

    Person[] people = new Person[]new Person("foo"), new Person("bar"), new Person("baz");
    Group aGroup = new Group(people);

    Group[] group = new Group[100];
    group[0] = aGroup;
    // group[100] = aGroup; -> Array index is out of bounds
    System.out.println("group:\n" + Arrays.toString(group) + "\n");

    Group[] group1 = new Group[1];
    group1[0] = new Group();
    group1[0].setGroupOfPerson(people);
    System.out.println("group1:\n" + Arrays.toString(group1) + "\n");

    Group[] group2 = new Group[]aGroup;
    System.out.println("group2:\n" + Arrays.toString(group2) + "\n");

    List<Group> l = List.of(new Group(people));
    Group[] group3 = l.toArray(Group[]::new);
    System.out.println("group3:\n" + Arrays.toString(group3) + "\n");

    Group[] group4 = new Group[]aGroup;
    group4[0].setGroupOfPerson(people);
    System.out.println("group4:\n" + Arrays.toString(group4) + "\n");

    Group[] group5 = new Group[]aGroup;
    group4[0].getGroupOfPerson()[0] = new Person("fooMod");
    System.out.println("group5:\n" + Arrays.toString(group5) + "\n");

    Group[] group6 = new Group[100];
    group6[0] = new Group(new Person[]new Person("foo"));
    System.out.println("group6:\n" + Arrays.toString(group6) + "\n");
   

这里是结果:

group:
[GroupgroupOfPerson=[Personname='foo', Personname='bar', Personname='baz'], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

group1:
[GroupgroupOfPerson=[Personname='foo', Personname='bar', Personname='baz']]

group2:
[GroupgroupOfPerson=[Personname='foo', Personname='bar', Personname='baz']]

group3:
[GroupgroupOfPerson=[Personname='foo', Personname='bar', Personname='baz']]

group4:
[GroupgroupOfPerson=[Personname='foo', Personname='bar', Personname='baz']]

group5:
[GroupgroupOfPerson=[Personname='fooMod', Personname='bar', Personname='baz']]

group6:
[GroupgroupOfPerson=[Personname='foo'], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]


Process finished with exit code 0

【讨论】:

【参考方案2】:

您可以通过以下任何一种方式进行操作:

class Person 



class Group 
    final int MAX = 10;
    private Person[] _groupOfPersons;
    private int _numOfPersons;

    public Group() 
        _groupOfPersons = new Person[MAX];
        _numOfPersons = 0;
    

    public Person[] get_groupOfPersons() 
        return _groupOfPersons;
    

    public void set_groupOfPersons(Person[] _groupOfPersons) 
        this._groupOfPersons = _groupOfPersons;
    

    public int get_numOfPersons() 
        return _numOfPersons;
    

    public void set_numOfPersons(int _numOfPersons) 
        this._numOfPersons = _numOfPersons;
    

    public void addPerson(Person person) 
        for (int i = 0; i < MAX; i++) 
            if (_groupOfPersons[i] == null) 
                _groupOfPersons[i] = person;
                return;
            
        
    


public class Main 
    public static void main(String[] args) 
        Group group = new Group();

        // First way
        group.get_groupOfPersons()[0] = new Person();

        // Second way
        group.addPerson(new Person());
    

【讨论】:

【参考方案3】:

如果你想在 main 中声明一个包含 2 列的表,请执行以下操作:

Group[] group1 = new Group2];

填满你可以做的列

group1[0].setPersons(..);
group1[1].setPersons(..);

【讨论】:

【参考方案4】:

Group[] group1 = new Group[];

这会初始化一个空数组。

如果你这样做:Group[] group1 = new Group[]new Group();

你可以group1[0].setPersons(...);

【讨论】:

以上是关于如何在main中初始化一个对象数组?的主要内容,如果未能解决你的问题,请参考以下文章

您好,请问java中tostring方法如何输出对象数组??

可变数组怎么初始化(可变大小的对象不能被初始化)?

如何在Java中初始化一个对象数组

Reactjs如何在状态中初始化数组对象

java中如何初始化对象数组,并增加元素

如何初始化动态对象数组