Java:创建对象数组的数组
我是Java新手,正在从事一个小项目,遇到了一个问题,希望您能提供帮助: 我试图创建一个二维数组,其中每个元素都是一个类型为Field的对象,其中包含x和y(作为元素的坐标) 当发送的长度和宽度参数<0时,我抛出了一个错误 我正在用main方法测试代码,但总是抛出错误,这意味着创建“map”的方法没有收到正确的参数 注意:main方法在另一个类(main类)中Java:创建对象数组的数组,java,arrays,exception,inner-classes,Java,Arrays,Exception,Inner Classes,我是Java新手,正在从事一个小项目,遇到了一个问题,希望您能提供帮助: 我试图创建一个二维数组,其中每个元素都是一个类型为Field的对象,其中包含x和y(作为元素的坐标) 当发送的长度和宽度参数0和长度>0){ 字段[][]映射=新字段[宽度][长度]; 对于(int i=0;i
```
导入bbb.MyException;
公共类协调系统{
私有整数长度;
私有整数宽度;
私有字段[][]map=createMap(getWidth(),getLength());
公共协调系统(int-width,int-length)抛出MyException{
这个。宽度=宽度;
这个长度=长度;
}
公共整数getLength(){
返回这个.length;
}
公共int getWidth(){
返回这个.width;
}
公共类字段{
私人INTX;
私营企业;
公共无效集合x(整数x){
这个.x=x;
}
公共int getX(){
返回x;
}
公共空间设置(整数y){
这个。y=y;
}
公共int getY(){
返回y;
}
}
公共字段[][]getMap(){
返回图;
}
//初始化每个“字段”的坐标
公共字段[][]createMap(int-width,int-length)引发MyException{
如果(宽度>0和长度>0){
字段[][]映射=新字段[宽度][长度];
对于(int i=0;i
线程“main”bbb.MyException中的异常:错误!抱歉,无法创建
宽度或高度字段=0
在CoordinateSystem.createMap(CoordinateSystem.java:62)
at CoordinateSystem.(CoordinateSystem.java:9)
在Main.Main(Main.java:21)
进程已完成,退出代码为1
这行代码(在方法createMap()
中)
Field[][]map=新字段[宽度][长度];
创建二维数组,但数组中的每个元素都为空。因此,这行代码(也在方法
createMap()
中)
map[i][j].setX(j);
将抛出一个NullPointerException
您需要显式创建字段
对象。另外,在
映射
中,一些字段
元素的Y坐标和一些元素的X坐标都是零,因为(同样在方法createMap()
中)你用零来启动for
循环。为了解决这个问题,我在调用setX()时向I
和j
添加了一个
和setY()
以下是方法createMap()
for(int i=0;i
只剩下调用方法createMap()
。由于map
是类CoordinateSystem
的成员,因此从CoordinateSystem
的构造函数调用createMap()
似乎是合乎逻辑的
公共协调系统(int-width,int-length)抛出MyException{
这个。宽度=宽度;
这个长度=长度;
map=createMap(宽度、长度);
}
最后,为了完整起见,这里是类CoordinateSystem
公共类协调系统{
私有整数长度;
私有整数宽度;
私有字段[][]映射;
公共协调系统(int-width,int-length)抛出MyException{
这个。宽度=宽度;
这个长度=长度;
map=createMap(宽度、长度);
}
公共整数getLength(){
返回这个.length;
}
公共int getWidth(){
返回这个.width;
}
公共类字段{
私人INTX;
私营企业;
公共无效集合x(整数x){
这个.x=x;
}
公共int getX(){
返回x;
}
公共空间设置(整数y){
这个。y=y;
}
公共int getY(){
返回y;
}
}
公共字段[][]getMap(){
返回图;
}
//初始化每个“字段”的坐标
公共字段[][]createMap(int-width,int-length)引发MyException{
如果(宽度>0和长度>0){
字段[][]映射=新字段[宽度][长度];
对于(int i=0;i
在初始化之前,您正在初始化map
```
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