java 如何初始化二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13707869/
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
How do I initialize a two-dimensional array?
提问by Sakit Atakishiyev
public class example
{
static class point
{
int x;
int y;
}
static void main(String args[])
{
point p = new point();
point[] p1 = new point[5];
point[][] p2 = new point[5][5];
p.x = 5; //No problem
p[0].x = 5; //When I run the program, it gives error:java.lang.NullPointerException
p[0][0].x = 5; //When I run the program, it gives error:java.lang.NullPointerException
}
How can I initialize p[].x and p[][].x?
如何初始化 p[].x 和 p[][].x?
回答by Matzi
You need to manually initialize the whole array and all levels if multi-leveled:
如果是多级,您需要手动初始化整个数组和所有级别:
point[] p1 = new point[5];
// Now the whole array contains only null elements
for (int i = 0; i < 5; i++)
p1[i] = new point();
p1[0].x = 1; // Will be okay
回答by Thor84no
Think of it this way; when you do new point[5]
(you should follow coding standards and name your classes with an upper case first letter btw.), you get an array with every element being the default value for that type(in this case null). The array is initialised, but if you want individual elements of the array to be initialised, you have to do that as well, either in the initial line like this:
这样想;当你这样做时new point[5]
(你应该遵循编码标准并用大写的第一个字母 btw 命名你的类),你会得到一个数组,其中每个元素都是该类型的默认值(在这种情况下为 null)。数组已初始化,但如果您想初始化数组的各个元素,您也必须这样做,或者在这样的初始行中:
point[] p1 = new point[] { new point(), new point() };
(The above method will create an array with each element already initialised of the minimum size that will accommodate those elements - in this case 2.)
(上述方法将创建一个数组,其中每个元素都已初始化为可容纳这些元素的最小大小 - 在本例中为 2。)
Or by looping through the array and adding the points manually:
或者通过循环遍历数组并手动添加点:
point[] p1 = new point[5];
for (int i = 0; i < p1.length; i++) {
point[i] = new point();
}
Both these concepts can be extended to multi-dimensional arrays:
这两个概念都可以扩展到多维数组:
point[] p2 = new point[][] {
new point[] { new point(), new point() }
new point[] { new point(), new point() }
};
Or
或者
point[] p2 = new point[5][5];
for (int i = 0; i < p2.length; i++) {
for (int j = 0; j < p2[i].length; j++) {
p2[i][j] = new point();
}
}
回答by Subhrajyoti Majumder
point p = new point();
It is the point
object.
它是point
对象。
point[] p1 = new point[5];
This point
object is an 1D array. It holds point
object references. You should create a point
object and keep into the array like -
这个point
对象是一个一维数组。它保存point
对象引用。您应该创建一个point
对象并保留在数组中,例如 -
for (int i = 0; i < 5; i++)
p1[i] = new point();
p1[0].x = 1;
And for a 2D array -
对于二维数组 -
point[][] p2 = new point[5][5];
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++)
p1[i][j] = new point();
}
p[0][0].x = 5;
回答by Dilum Ranatunga
When you construct an array of objects, the array itself is constructed, but the individual elements are initialized to null. So, assuming Point()
is the constructor you want,
构造对象数组时,会构造数组本身,但将各个元素初始化为 null。所以,假设Point()
是你想要的构造函数,
Point[] p1 = new Point[5];
for (int i = 0; i < p1.length; ++i) {
p1[i] = new Point();
}