我是Java的新手,在Java中创建了一个对象数组。

我有一个a类的例子

A[] arr = new A[4];

但这只是创建指向A的指针(引用),而不是4个对象。这对吗?我看到,当我试图访问创建的对象中的函数/变量时,我得到一个空指针异常。 为了能够操作/访问对象,我必须这样做:

A[] arr = new A[4];
for (int i = 0; i < 4; i++) {
    arr[i] = new A();
}

这是正确的还是我做错了什么?如果这是正确的,那真的很奇怪。

编辑:我觉得这很奇怪,因为在c++中,你只需输入new A[4],它就创建了四个对象。


当前回答

这是正确的。

A[] a = new A[4];

...创建4个A引用,类似于这样做:

A a1;
A a2;
A a3;
A a4;

现在你不能执行a1. somemethod()而不像这样分配a1:

a1 = new A();

类似地,对于数组,你需要这样做:

a[0] = new A();

...使用前。

其他回答

假设A类如下:

class A{
int rollno;
int DOB;
}        

你想为类a创建一个对象数组,你这样做,

    A[] arr = new A[4];    //Statement 1
    for (int i = 0; i < 4; i++) {
    arr[i] = new A();      //Statement 2
    }

这完全正确。

Here A is the class and in Statement 1 Class A is a datatype of the array. When this statement gets executed because of the new keyword an object is created and dynamically memory is allocated to it which will be equal to the space required for the 4 blocks of datatype A i.e, ( for one block in the array space required is 8 bytes (4+4), I am assuming int takes 4 bytes of space. therefore total space allocated is 4*4 bytes for the array ). Then the reference of the object is given to the arr variable. Here important point to note is that Statement 1 has nothing to do with creating an object for class A ,no object is created for this class it is only used as a datatype which gives the size of the class A required for the memory allocation of the array.

然后,当for循环运行和语句2执行时,JVM现在为类A分配内存(即创建一个对象),并将其引用给arr[i]。每次调用循环时,都会创建一个对象,并将其引用给arr[i]。

因此,拥有8字节空间的arr[0]被赋予a类对象的引用,每次运行time循环都会创建一个新对象,并给予该对象引用,以便它现在可以访问该对象中的数据。

下面是创建一个包含10个雇员对象的数组的清晰示例,并带有一个带形参的构造函数:

public class MainClass
{  
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        //step1 : first create array of 10 elements that holds object addresses.
        Emp[] employees = new Emp[10];
        //step2 : now create objects in a loop.
        for(int i=0; i<employees.length; i++){
            employees[i] = new Emp(i+1);//this will call constructor.
        }
    }
}

class Emp{
    int eno;
    public Emp(int no){
        eno = no;
        System.out.println("emp constructor called..eno is.."+eno);
    }
}

在java中声明一个新数组的一般形式如下:

type arrayName[] = new type[numberOfElements];

其中type是基本类型或Object。numberOfElements是您将存储到数组中的元素数量,这个值不能更改,因为Java不支持动态数组(如果您需要一个灵活的动态结构来保存对象,您可能需要使用一些Java集合)。

让我们初始化一个数组来存储一个5人的小公司中所有员工的工资:

Int salary [] = new Int [5];

数组的类型(在本例中为int)适用于数组中的所有值。不能在一个数组中混合类型。

现在我们已经初始化了工资数组,我们想要在其中放入一些值。我们可以在初始化过程中这样做:

Int salary [] = {50000, 75340, 110500, 98270, 39400};

或者在以后这样做:

salaries[0] = 50000;
salaries[1] = 75340;
salaries[2] = 110500;
salaries[3] = 98270;
salaries[4] = 39400;

更多数组创建的可视化示例:

要了解更多关于数组的知识,请查看指南。

这是正确的。你还可以:

A[] a = new A[] { new A("args"), new A("other args"), .. };

此语法也可以用于在任何地方创建和初始化数组,例如在方法参数中:

someMethod( new A[] { new A("args"), new A("other args"), . . } )

对于泛型类,有必要创建包装器类。 例如:

Set<String>[] sets = new HashSet<>[10]

结果:"不能创建通用数组"

使用:

        class SetOfS{public Set<String> set = new HashSet<>();}
        SetOfS[] sets = new SetOfS[10];