尝试在Java中打印2d元素数组

尝试在Java中打印2d元素数组,java,Java,在我正在制作的名为Flood It的游戏中,我在打印我正在制作的对象时遇到了问题,这些对象的构造函数是: Dot(int x, int y, int color) 我有三个类一个类是DotInfo.java: public class DotInfo { private int x; private int y; private int color; private boolean captured; public DotInfo(int x, int y, int color){

在我正在制作的名为Flood It的游戏中,我在打印我正在制作的对象时遇到了问题,这些对象的构造函数是:

Dot(int x, int y, int color)
我有三个类一个类是DotInfo.java:

public class DotInfo {


private int x;
private int y;
private int color;
private boolean captured;

public DotInfo(int x, int y, int color){
    this.x = x;
    this.y = y;
    this.color = color;
    captured = false;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}


public void setCaptured(boolean captured) {
    this.captured = captured;
}

public boolean isCaptured(){
    return captured;
}

public int getColor() {
    return color;
}

}
我的第二个类是GameModel.java:

import java.util.Random;

public class GameModel {


/**
 * predefined values to capture the color of a DotInfo
 */
public static final int COLOR_0           = 0;
public static final int COLOR_1           = 1;
public static final int COLOR_2           = 2;
public static final int COLOR_3           = 3;
public static final int COLOR_4           = 4;
public static final int COLOR_5           = 5;
public static final int NUMBER_OF_COLORS  = 6;

public static DotInfo[][] dots;
private DotInfo dot;
private int size;
private int currentColor;
private Random generator;
private int steps;
private String rep;

/**
 * Constructor to initialize the model to a given size of board.
 * 
 * @param size
 *            the size of the board
 */
public GameModel(int size) {
    this.size = size;
}


/**
 * Resets the model to (re)start a game. The previous game (if there is one)
 * is cleared up . 
 */
public void reset(){
    int color = 0;
    dots = new DotInfo[size][size];
    for (int i=0;i<size;i++) {
        for (int j=0;j<size;j++) {
            new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
            System.out.println(dots[i][j]);
        } 
    }
}


/**
 * Getter method for the size of the game
 * 
 * @return the value of the attribute sizeOfGame
 */   
public int getSize(){
    return size;
}

/**
 * returns the current color  of a given dot in the game
 * 
 * @param i
 *            the x coordinate of the dot
 * @param j
 *            the y coordinate of the dot
 * @return the status of the dot at location (i,j)
 */   
public int getColor(int i, int j){
    int color=0;
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            if (dots[x][y].getX()==i && dots[x][y].getY()==j) {
                color=dots[x][y].getColor();
            }
        }
    }
    return color;
}

/**
 * returns true is the dot is captured, false otherwise
* 
 * @param i
 *            the x coordinate of the dot
 * @param j
 *            the y coordinate of the dot
 * @return the status of the dot at location (i,j)
 */   
public boolean isCaptured(int i, int j){
    boolean capture = true;
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            if (dots[x][y].getX()==i && dots[x][y].getY()==j) {
                capture=dots[x][y].isCaptured();
            }
        }
    }
    return capture;   
}

/**
 * Sets the status of the dot at coordinate (i,j) to captured
 * 
 * @param i
 *            the x coordinate of the dot
 * @param j
 *            the y coordinate of the dot
 */   
public void capture(int i, int j){
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            if (dots[x][y].getX()==i && dots[x][y].getY()==j) {
                dots[x][y].setCaptured(true);
            }
        }
    }  
}


/**
 * Getter method for the current number of steps
 * 
 * @return the current number of steps
 */   
public int getNumberOfSteps(){
    return steps;
}

/**
 * Setter method for currentSelectedColor
 * 
 * @param val
 *            the new value for currentSelectedColor
*/   
public void setCurrentSelectedColor(int val) {
    currentColor = val;
}

/**
 * Getter method for currentSelectedColor
 * 
 * @return currentSelectedColor
 */   
public int getCurrentSelectedColor() {
    return currentColor;
}


/**
 * Getter method for the model's dotInfo reference
 * at location (i,j)
 *
  * @param i
 *            the x coordinate of the dot
 * @param j
 *            the y coordinate of the dot
 *
 * @return model[i][j]
 */   
public DotInfo get(int i, int j) {
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            if (dots[x][y].getX()==i && dots[x][y].getY()==j) {
                dot = dots[x][y];
            }
        }
    }
    return dot;
}


 /**
 * The metod <b>step</b> updates the number of steps. It must be called 
 * once the model has been updated after the payer selected a new color.
 */
 public void step(){
    steps++;
}

 /**
 * The metod <b>isFinished</b> returns true iff the game is finished, that
 * is, all the dats are captured.
 *
 * @return true if the game is finished, false otherwise
 */
public boolean isFinished(){
    boolean flag=true;
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            if (dots[x][y].isCaptured()==false) {
                flag=false;
            }
        }
    }
    return flag;
}


 /**
 * Builds a String representation of the model
 *
 * @return String representation of the model
 */
public String toString(){
    for (int x=0;x<size;x++) {
        for (int y=0;y<size;y++) {
            rep += "Dot("+dots[x][y].getX()+", "+ dots[x][y].getY()+", "+dots[x][y].getColor()+")";
        }
    }
    return rep;
}
}
import java.util.Random;
公共类博弈模型{
/**
*捕获点信息颜色的预定义值
*/
公共静态最终整数颜色_0=0;
公共静态最终整数颜色_1=1;
公共静态最终整数颜色_2=2;
公共静态最终整数颜色_3=3;
公共静态最终整数颜色_4=4;
公共静态最终整数颜色_5=5;
公共静态最终整数颜色=6;
公共静态点信息[][]点;
私有点信息点;
私有整数大小;
私人色彩;
专用随机发生器;
私有int步骤;
私有字符串代表;
/**
*构造函数将模型初始化为给定大小的板。
* 
*@param大小
*董事会的规模
*/
公共游戏模型(整数大小){
这个。大小=大小;
}
/**
*重置模型以(重新)开始游戏。上一个游戏(如果有)
*一切都搞定了。
*/
公共无效重置(){
int color=0;
点=新点信息[大小][大小];

对于(int i=0;i您忘记将新的DotInfo()对象分配给数组的每个位置

该行:

new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
应该是:

dots[i][j] = new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);

在重置方法中,您有一个for循环,用于执行以下操作:

 dots = new DotInfo[size][size];
    for (int i=0;i<size;i++) {
        for (int j=0;j<size;j++) {
            new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
            System.out.println(dots[i][j]);
        } 
    }
他什么也不做

您正在创建一个对象,是的,但没有将其分配给任何对象,因此该对象正在丢失

您必须将该新对象指定给二维数组中的元素

dots[i][j] = new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
编辑: 另一方面,您需要初始化generator对象:

public GameModel(int size) {
    this.size = size;
    generator = new Random();

}

刚刚编辑它,现在它说这行是错误的,我仍然得到一个NullPointerException。试试看,让我知道!
dots[i][j] = new DotInfo(i, j, generator.nextInt(NUMBER_OF_COLORS)+1);
public GameModel(int size) {
    this.size = size;
    generator = new Random();

}