Java,使用点数组

发布于 2024-09-27 03:07:43 字数 363 浏览 6 评论 0原文

我正在用Java编写一个程序,其中我定义了一个类

class Point
{
    double x;
    double y;
}

中,我定义了一个点数组,如下所示:

Point[]     line = new Point[6];

然后在一个方法

line[SampleSize - i + 1].x = i;

, 数组索引为1; 但此时程序抛出空指针异常。

这似乎是索引对象字段的正确方法 对象数组。我做错了什么?

预先感谢您的任何建议。

约翰·多纳

I am writing a program in Java, in which I define a class

class Point
{
    double x;
    double y;
}

Then in a method, I define an array of points, as follows:

Point[]     line = new Point[6];

In the same method, I have the line

line[SampleSize - i + 1].x = i;

The first time this statement is hit, the value of its array index is 1;
but the program throws a null pointer exception at this point.

This would seem like the correct way to index the field of an object within an
array of objects. What am I doing wrong?

Thanks in advance for any suggestions.

John Doner

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

没有你我更好 2024-10-04 03:07:43

您必须在访问该值之前对其进行初始化:

line[SampleSize - i + 1] = new Point();

You have to initialize the value before accessing it:

line[SampleSize - i + 1] = new Point();
千鲤 2024-10-04 03:07:43

那是因为您还没有创建要放入数组中的点

for (int index = 0; index < line.length; index++)
{
    line[index] = new Point();
}

That's because you have not created the points to put in the array

for (int index = 0; index < line.length; index++)
{
    line[index] = new Point();
}
土豪 2024-10-04 03:07:43
Point[]     line = new Point[6];

创建一个空数组,能够保存 Points。但还没有任何对 Point 的引用。全部为空。

line[SampleSize - i + 1].x = i; 

尝试在 null 上访问 x

Point[]     line = new Point[6];

creates an empty array, capable of holding Points. But does not hold any reference to a Point yet. All are null.

line[SampleSize - i + 1].x = i; 

tries to access x on a null.

奢华的一滴泪 2024-10-04 03:07:43

只是为了添加鲍里斯的答案,这里有一些代码

class Point {
    double x;
    double y;
}


Point[] line = new Point[6];
for(int i = 0; i < line.length; i++) {
    line[i] = new Point();
}

    // now you can set the values, since the point's aren't null
line[0].x = 10;
line[0].y = 10;

Just to add to Boris's answer, here is some code

class Point {
    double x;
    double y;
}


Point[] line = new Point[6];
for(int i = 0; i < line.length; i++) {
    line[i] = new Point();
}

    // now you can set the values, since the point's aren't null
line[0].x = 10;
line[0].y = 10;
冷了相思 2024-10-04 03:07:43

http://java.sun.com/docs/books/jls /third_edition/html/arrays.html

如果您注意到第 10.2 节中创建数组的行为只是创建引用,而不是对象。因此,空指针错误的原因是,所有引用都被赋予默认值,在本例中为空。

http://java.sun.com/docs/books/jls/third_edition/html/arrays.html

If you note in Section 10.2 the act of creating an array simply creates the references, but not the objects. Hence the reason for your null pointer error, all of the references are given the default value, which in this case is null.

—━☆沉默づ 2024-10-04 03:07:43

尽管您已经分配了数组,但数组的内容为空。你需要做什么:

Point[] line = new Point[6];
for (int i = 0; i < line.length; i++)
{
    line[i] = new Point();
}

Although you have allocated the array, the contents of the array are null. What you need to do:

Point[] line = new Point[6];
for (int i = 0; i < line.length; i++)
{
    line[i] = new Point();
}
祁梦 2024-10-04 03:07:43

首次创建数组时,它包含六个 null 引用。

在与数组中的对象交互之前,您需要创建对象,如下所示:

line[someIndex] = new Point();

您可能希望使用 for 循环初始化数组中的每个点。

When you first create the array, it contains six null references.

Before you can interact with objects in the array, you need to create the objects, like this:

line[someIndex] = new Point();

You probably want to initialize every point in the array using a for loop.

零度℉ 2024-10-04 03:07:43

java 有一个内置的 Point 类

import java.awt.Point;

https://docs .oracle.com/javase/7/docs/api/java/awt/Point.html

java has a built in Point class

import java.awt.Point;

https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文