Java:定义一个用户定义结构的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9706197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:27:34  来源:igfitidea点击:

Java: defining an array of user-defined struct

java

提问by C graphics

I used to write my program codes in C and now moving to Java trying to define a simple struct and create and array of it in the code below, however the run-time exception occurs Exception in thread "main" java.lang.NullPointerException at CPoint.main(CPoint.java:19)I know i have to allocate memory to my array somewhere, but do not know where. Any help will be appreciated:

我曾经用 C 编写我的程序代码,现在转向 Java 尝试定义一个简单的结构并在下面的代码中创建它的数组,但是运行时异常发生 在线程“main”java.lang.NullPointerException 中CPoint.main(CPoint.java:19)我知道我必须在某个地方为我的数组分配内存,但不知道在哪里。任何帮助将不胜感激:

  public class CPoint {
public int x;
public int y;

public CPoint(int size){

    System.out.println("Constructor1 is called");
}
    public CPoint(){
    System.out.println("Constructor2 is called");
}


public static void main(String[] args){

    CPoint [] p = new CPoint [3];

    p[0].x=90;p[0].y=80;        // this is line 19

    System.out.println(p[0].x);
}

}

}

PS. I would like to allocate memory somewhere in the class CPoint , not in main, if possible I would like to keep the main() code as simple as possible

附注。我想在类 CPoint 中的某处分配内存,而不是在 main 中,如果可能的话,我想让 main() 代码尽可能简单

回答by Moonbeam

Array pcontains 3 nullentries. Initializing an array will not create the objects therein for you. For more information, see Arrays.

数组p包含 3null个条目。初始化数组不会为您创建其中的对象。有关更多信息,请参阅数组

回答by Aleks G

You need to allocate each of the objects in the array:

您需要分配数组中的每个对象:

public static void main(String[] args){

    CPoint [] p = new CPoint [3];

    p[0] = new CPoint();
    p[0].x=90;p[0].y=80;

    System.out.println(p[0].x);
}

Edit:You can wrap array initialisation into a static method in the class - essentially an array factory method:

编辑:您可以将数组初始化包装到类中的静态方法中 - 本质上是一个数组工厂方法:

public class CPoint {
    public int x;
    public int y;

    public CPoint() { System.out.println("Inside constructor 1"); }

    public static CPoint[] CPointSet(int size) {
        CPoint[] p= new CPoint[size];
        for(int i=0; i<size; i++)
            p[i] = new CPoint();
        return p;
    }

    public static void main(String[] args) {
        CPoint[] p = CPoint.CPointSet(3);
        p[0].x = 90;
        p[0].y = 80;
    }
}

回答by Peter Lawrey

You need to create your objects before your use them.

您需要在使用对象之前创建对象。

CPoint [] p = new CPoint [3];
p[0] = new CPoint();

回答by xinthink

Agree to Moonbeam, new CPoint[3] will create an Array object, which in fact an array of references(that's different to C), and all three elements refer to 'null' initially. No memory (for CPoint struct) is allocated util you new some CPoint objects.

同意 Moonbeam, new CPoint[3] 将创建一个 Array 对象,它实际上是一个引用数组(与 C 不同),并且所有三个元素最初都引用 ' null'。没有内存(对于 CPoint 结构)分配给你新的一些 CPoint 对象。

You can initialize arrays in this concise way:

您可以以这种简洁的方式初始化数组:

CPoint[] p = { new CPoint(), new CPoint(), new CPoint() }

p[0].x=90; p[0].y=80;

System.out.println(p[0].x);