Java : 创建对象数组的数组
Java : creating an array of array of objects
我是 Java 的新手,正在做一个小项目并遇到问题,希望您能提供帮助:
我正在尝试创建一个二维数组,其中每个元素都是一个字段类型的对象,其中包含 x 和 y(作为元素的坐标)
当发送的长度和宽度参数小于 0 时我抛出错误
我正在 main 方法中测试我的代码,但总是抛出错误,这意味着创建“地图”的方法没有收到正确的参数。
注意:主要方法在不同的 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
你的这行代码(在方法 createMap()
中)...
Field[][] map = new Field[width][length];
创建一个二维数组,但数组中的每个元素都是空的。
因此,您的这行代码(也在方法 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
是 class CoordinateSystem
的成员,因此从 CoordinateSystem
.
的构造函数中调用 createMap()
似乎是合乎逻辑的
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
最后,为了完整起见,这里是 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());
}
}
}
}
您在初始化 width
和 height
之前初始化 map
,因此 getWidth()
和 getHeight()
都 return 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);
}
// rest of the class...
我是 Java 的新手,正在做一个小项目并遇到问题,希望您能提供帮助:
我正在尝试创建一个二维数组,其中每个元素都是一个字段类型的对象,其中包含 x 和 y(作为元素的坐标)
当发送的长度和宽度参数小于 0 时我抛出错误
我正在 main 方法中测试我的代码,但总是抛出错误,这意味着创建“地图”的方法没有收到正确的参数。
注意:主要方法在不同的 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
你的这行代码(在方法 createMap()
中)...
Field[][] map = new Field[width][length];
创建一个二维数组,但数组中的每个元素都是空的。
因此,您的这行代码(也在方法 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
是 class CoordinateSystem
的成员,因此从 CoordinateSystem
.
createMap()
似乎是合乎逻辑的
public CoordinateSystem(int width, int length) throws MyException {
this.width = width;
this.length = length;
map = createMap(width, length);
}
最后,为了完整起见,这里是 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());
}
}
}
}
您在初始化 width
和 height
之前初始化 map
,因此 getWidth()
和 getHeight()
都 return 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);
}
// rest of the class...