[英]How to properly instantiate a bi-dimensional array in Java
I am having problems on how to instantiate bi-dimensional array of objects. 我在如何实例化对象的二维数组时遇到问题。 I tried to demonstrate below a small sample to reproduce the error I am getting.
我试图在下面演示一个小样本来重现我得到的错误。
I have this class named Node that basically stores one character. 我有一个名为Node的类,它基本上存储一个字符。 This class is used inside the class named Test as a bi-dimensional attribute.
此类在名为Test的类内用作二维属性。 I used some user input to establish the size of the array and instantiate it inside the instantiate() method.
我使用了一些用户输入来建立数组的大小,并在Instantiate()方法中实例化它。 Then, I try to populate the map using a set method.
然后,我尝试使用set方法填充地图 。 However, the compiler gives me the following error message:
但是,编译器给我以下错误消息:
Eclipse Console Output: Eclipse控制台输出:
Exception in thread "main" java.lang.NullPointerException at Test.populate(Main.java:44) at Main.main(Main.java:77)
Main.main(Main.java:77)的Test.populate(Main.java:44)处的线程“ main”中的java.lang.NullPointerException异常
My input was: 我的输入是:
Enter height: 3 Enter width: 3
输入高度:3输入宽度:3
Below is the code I am using to reproduce this error: 下面是我用来重现此错误的代码:
Class Node: 类节点:
class Node {
private char content;
Node(){
this.content = ' ';
}
Node(Node node){
this.content = node.getContent();
}
//Setter
public void setContent(char c) {
this.content = c;
}
//Getter
public char getContent() {
return this.content;
}
}
Class Test: 课堂测试:
class Test {
private Node[][] map;
private int height, width;
public void instantiate(){
Scanner reader = new Scanner(System.in);
System.out.println("Enter height: ");
this.height = reader.nextInt();
System.out.println("Enter width: ");
this.width = reader.nextInt();
map = new Node[height][width];
reader.close();
}
public void populate(){
for(int i=0;i<height;i++)
for(int j=0;j<width;j++){
if((i+j) %2 == 0)
map[i][j].setContent('a');
else
map[i][j].setContent('b');
}
/*
* a b a b
* b a b a ...
* a b a b
* b a b a
* . . .
*/
}
public void print(){
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
System.out.print(map[i][j].getContent());
}
System.out.println();
}
}
public Node[][] getMap(){
return this.map;
}
}
Main method: 主要方法:
public class Main {
public static void main(String[] args) {
Test testing = new Test();
testing.instantiate();
testing.populate();
testing.print();
}
}
The code can also be seen here: http://pastebin.com/agFMmB38 该代码也可以在这里看到: http : //pastebin.com/agFMmB38
I am still getting used to Java (coming from C++), so they have some differences that I couldn't figure it out yet. 我仍然习惯Java(来自C ++),因此它们之间存在一些差异,我尚无法弄清。
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you!
谢谢!
map[i][j]
needs to be set to a new object, like map[i][j] = new Node('a')
(well, if you had a constructor in Node
which worked like that: it would be written Node(char a) { this.content = a; }
). 需要将
map[i][j]
设置为一个新对象,例如map[i][j] = new Node('a')
(好吧,如果Node
有一个像这样工作的构造函数:书面Node(char a) { this.content = a; }
)。
You cannot do map[i][j].setContent('a')
because it is not a preexisting Node
object. 您无法执行
map[i][j].setContent('a')
因为它不是预先存在的Node
对象。
You need to init an Node object before use setContent(char c) method. 在使用setContent(char c)方法之前,您需要初始化一个Node对象。
public void populate() {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
map[i][j] = new Node();
if((i+j) %2 == 0)
map[i][j].setContent('a');
else
map[i][j].setContent('b');
}
}
}
Then use method getContent() to retrieve value what you want. 然后使用方法getContent()检索所需的值。
public void print() {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++) {
System.out.print(map[i][j].getContent());
}
System.out.println();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.