英文:
Java : creating an array of array of objects
问题
import bbb.MyException;
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map = createMap(getWidth(), getLength());
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
}
public int getLength() {
return this.length;
}
public int getWidth() {
return this.width;
}
public class Field{
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
public Field[][] getMap() {
return map;
}
// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
if(width > 0 && length > 0){
Field[][] map = new Field[width][length];
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j].setX(j);
map[i][j].setY(i);
}
}
return map;
} else{
throw new MyException("Sorry, can't create a field of width or height = 0 ");
}
}
}
public static void main(String[] args) throws MyException {
CoordinateSystem board = new CoordinateSystem(8, 9);
for( int i = 0 ; i < 8 ; i++ ){
for( int j = 0 ; j < 9 ; j++ ){
System.out.print(board.getMap()[i][j].getX());
System.out.println(board.getMap()[i][j].getY());
}
}
}
Exception in thread "main" bbb.MyException: Error! Sorry, can't create a
field of width or height = 0
at CoordinateSystem.createMap(CoordinateSystem.java:62)
at CoordinateSystem.
at Main.main(Main.java:21)
Process finished with exit code 1
英文:
i'm new in Java and working on a small project and having an issue, I hope you can help :
I'm trying to create a 2 dimensional array, inwhich each element is an Object of type Field that holds x and y ( as the coordinate of the element )
I'm throwing an error when the sent parameters for length and width are < 0
I'm testing my code in the main method but the error is always thrown meaning that the method to create the "map" is not receiving the correct parameters.
Note : the main method is in a different class ( main class )
```
import bbb.MyException;
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map = createMap(getWidth(), getLength());
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
}
public int getLength() {
return this.length;
}
public int getWidth() {
return this.width;
}
public class Field{
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
public Field[][] getMap() {
return map;
}
// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
if(width > 0 && length > 0){
Field[][] map = new Field[width][length];
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j].setX(j);
map[i][j].setY(i);
}
}
return map;
} else{
throw new MyException("Sorry, can't create a field of width or height = 0 ");
}
}
}
public static void main(String[] args) throws MyException {
CoordinateSystem board = new CoordinateSystem(8, 9);
for( int i = 0 ; i < 8 ; i++ ){
for( int j = 0 ; j < 9 ; j++ ){
System.out.print(board.getMap()[i][j].getX());
System.out.println(board.getMap()[i][j].getY());
}
}
Exception in thread "main" bbb.MyException: Error! Sorry, can't create a
field of width or height = 0
at CoordinateSystem.createMap(CoordinateSystem.java:62)
at CoordinateSystem.<init>(CoordinateSystem.java:9)
at Main.main(Main.java:21)
Process finished with exit code 1
</details>
# 答案1
**得分**: 2
这行代码(在方法`createMap()`中)...
```java
Field[][] map = new Field[width][length];
创建了一个二维数组,但数组中的每个元素都是空的(null)。
因此,你代码中的这行(同样在方法createMap()中)...
map[i][j].setX(j);
会抛出一个 NullPointerException 异常。
你需要显式地创建 Field 对象。
另外,map 中某些 Field 元素的 Y 坐标以及某些元素的 X 坐标也是零,因为(同样在方法createMap()中)你在 for 循环中以零开始。为了修复这个问题,在调用 setX() 和 setY() 时,我将 i 和 j 都加一。
下面是方法 createMap() 中 for 循环的已修正代码
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}
唯一剩下的就是调用 createMap() 方法了。由于 map 是 CoordinateSystem 类的成员,从 CoordinateSystem 的构造函数中调用 createMap() 似乎是合乎逻辑的。
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
最后,为了完整起见,以下是整个 [修正后] 的 CoordinateSystem 类代码
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map;
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
public int getLength() {
return this.length;
}
public int getWidth() {
return this.width;
}
public class Field {
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
public Field[][] getMap() {
return map;
}
// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
if(width > 0 && length > 0){
Field[][] map = new Field[width][length];
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}
return map;
}
else{
throw new Exception("Sorry, can't create a field of width or height = 0 ");
}
}
public static void main(String[] args) throws MyException {
CoordinateSystem board = new CoordinateSystem(8, 9);
for( int i = 0 ; i < 8 ; i++ ) {
for( int j = 0 ; j < 9 ; j++ ) {
System.out.print(board.getMap()[i][j].getX());
System.out.println(board.getMap()[i][j].getY());
}
}
}
}
英文:
This line of your code (in method createMap())...
Field[][] map = new Field[width][length];
creates a two dimensional array but every element in the array is null.
Hence this line of your code (also in method createMap())
map[i][j].setX(j);
will throw a NullPointerException.
You need to explicitly create Field objects.
Also the Y coordinate of some of the Field elements in the map is zero as well as the X coordinate in some of the elements because (also in method createMap()) you start the for loops with zero. In order to fix that I add one to i and j when I call setX() and setY().
Here is the corrected code for the for loops in method createMap()
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}
The only thing left to do is call method createMap(). Since map is a member of class CoordinateSystem, it seems logical to call createMap() from the constructor of CoordinateSystem.
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
Finally, for the sake of completeness, here is the entire [corrected] code of class CoordinateSystem
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map;
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
public int getLength() {
return this.length;
}
public int getWidth() {
return this.width;
}
public class Field {
private int x;
private int y;
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
}
public Field[][] getMap() {
return map;
}
// Initializing a coordinate to each "field"
public Field[][] createMap(int width, int length) throws MyException {
if(width > 0 && length > 0){
Field[][] map = new Field[width][length];
for( int i = 0 ; i < width ; i++ ){
for( int j = 0 ; j < length ; j++ ){
map[i][j] = new Field();
map[i][j].setX(j + 1);
map[i][j].setY(i + 1);
}
}
return map;
}
else{
throw new Exception("Sorry, can't create a field of width or height = 0 ");
}
}
public static void main(String[] args) throws MyException {
CoordinateSystem board = new CoordinateSystem(8, 9);
for( int i = 0 ; i < 8 ; i++ ) {
for( int j = 0 ; j < 9 ; j++ ) {
System.out.print(board.getMap()[i][j].getX());
System.out.println(board.getMap()[i][j].getY());
}
}
}
}
答案2
得分: 0
你在初始化map之前就初始化了width和height,所以getWidth()和getHeight()都返回0。你可以将初始化放在构造函数内部,并在那里使用width和height:
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map;
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, height);
}
// 类的其余部分...
英文:
You're initializing map before initializing width and height, so getWidth() and getHeight() both return 0. You can move the initialization inside the constructor and use the width and height there:
public class CoordinateSystem {
private int length;
private int width;
private Field[][] map;
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, height);
}
// rest of the class...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论