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

How to initialise an array of object in main?

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

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() \????

Group[] group1 = new Group[]{};

这会初始化一个空数组。

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

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

如果你想声明一个 table 有 2 列,例如在 main 中,做:

Group[] group1 = new Group{2];

填写您可以做的栏目

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

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

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());
    }
}

方法真的很多:

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:
[Group{groupOfPerson=[Person{name='foo'}, Person{name='bar'}, Person{name='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:
[Group{groupOfPerson=[Person{name='foo'}, Person{name='bar'}, Person{name='baz'}]}]

group2:
[Group{groupOfPerson=[Person{name='foo'}, Person{name='bar'}, Person{name='baz'}]}]

group3:
[Group{groupOfPerson=[Person{name='foo'}, Person{name='bar'}, Person{name='baz'}]}]

group4:
[Group{groupOfPerson=[Person{name='foo'}, Person{name='bar'}, Person{name='baz'}]}]

group5:
[Group{groupOfPerson=[Person{name='fooMod'}, Person{name='bar'}, Person{name='baz'}]}]

group6:
[Group{groupOfPerson=[Person{name='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