单元模型输出未显示

时间:2015-12-03 15:47:48

标签: java oop multidimensional-array output

所以我正在研究一种非常基础的面向对象的Java细胞增长模型。下面是我的Cell类,它编译得很好,没有错误。

package cancerModel;
public class Cell {

public int x; //x position of cell
public int y; //y position of cell
public char type; //type of cell


public Cell(int x, int y, char type)
{
    this.x = x;
    this.y = y;
    this.type = type;
}

public void addCellToGrid(char[][] grid, int x, int y, char type)
{
    grid[x][y] = type;
}


public boolean checkAnySpace4(char[][] grid, int x, int y)
{
    if(grid[x-1][y] == 'N' || grid[x-1][y] == 'M'|| grid[x-1][y] == 'C')
    {
        return false;
    }
    if(grid[x-1][y-1] == 'N' || grid[x-1][y-1] == 'M'|| grid[x][y-1] == 'C')
    {
        return false;
    }
    if(grid[x][y-1] == 'N' || grid[x][y-1] == 'M'|| grid[x][y-1] == 'C')
    {
        return false;
    }
    if(grid[x+1][y] == 'N' || grid[x+1][y] == 'M'|| grid[x+1][y] == 'C')
    {
        return false;
    }
    else 
        return true;
}


public boolean checkAnySpace8(char[][] grid, int x, int y)
{
    if(grid[x-1][y] == 'N' || grid[x-1][y] == 'M'|| grid[x-1][y] == 'C')
    {
        return false;
    }
    if(grid[x-1][y-1] == 'N' || grid[x-1][y-1] == 'M'|| grid[x-1][y-1] == 'C')
    {
        return false;
    }
    if(grid[x][y-1] == 'N' || grid[x][y-1] == 'M'|| grid[x][y-1] == 'C')
    {
        return false;
    }
    if(grid[x+1][y] == 'N' || grid[x+1][y] == 'M'|| grid[x+1][y] == 'C')
    {
        return false;
    }
    if(grid[x][y+1] == 'N' || grid[x][y+1] == 'M'|| grid[x][y+1] == 'C')
    {
        return false;
    }
    if(grid[x+1][y+1] == 'N' || grid[x+1][y+1] == 'M'|| grid[x+1][y+1] == 'C')
    {
        return false;
    }
    if(grid[x+1][y-1] == 'N' || grid[x+1][y-1] == 'M'|| grid[x+1][y-1] == 'C')
    {
        return false;
    }
    if(grid[x-1][y+1] == 'N' || grid[x-1][y+1] == 'M'|| grid[x-1][y+1] == 'C')
    {
        return false;
    }
    else 
        return true;
}
public void divide(char[][] grid, char type, int x, int y)
{
    if(type == 'N')
    {
        grid[x-1][y] = 'N';
        grid[x][y-1] = 'N';
        grid[x-1][y-1] = 'N';
        grid[x+1][y] = 'N';
    }
     if (type == 'M')
    {
        grid[x-1][y] = 'M';
        grid[x][y-1] = 'M';
        grid[x-1][y-1] = 'M';
        grid[x+1][y] = 'M';
    }
    if(type =='C')
    {
        grid[x-1][y] = 'C';
        grid[x][y-1] = 'C';
        grid[x-1][y-1] = 'C';
        grid[x+1][y] = 'C';
        grid[x][y+1] = 'C';
        grid[x+1][y+1] = 'C';
        grid[x+1][y-1] = 'C';
        grid[x-1][y+1] = 'C';   
    }   
}
public void die(char[][] grid, int x, int y)
{
    grid[x][y] = ' ';
}   

public void mutate(char[][] grid,char type, int x, int y)
{
    if(type == 'N')
    {
        grid[x][y] = 'M';
    }
    else if (type == 'M')
    {
        grid[x][y] = 'C';
    }
 }
}

接下来,我有一个名为Setup的类,它假设初始化模型。 start()函数用于保存模型进展的逻辑,但尚未完全实现。在这一点上,我只是想确保我能够查看网格,所以我制作10个单元格并将它们放在网格中的随机点上。

package cancerModel;
public class Setup {

public double m = .05; //mutation rate
public double e = .6 ; //survival rate (1 - e = death rate)
public double p_c = .7; //growth rate for cancerous cells
public double p_n = .8; //growth rate for normal/mutated cells




public void printGrid(char[][] grid)
{
    for(int i = 0; i < 50; i++)
    {
        for(int j = 0; j < 50; j++)
        {
            System.out.println(grid[i][j]);
        }
    }
}

public void start(int steps)
{
    char[][] grid = new char[50][50];
    int random = (int)(Math.random() * 50 + 1);
    int random2 = (int)(Math.random() * 50 + 1);
    for(int i = 1; i < steps; i++)
    {
        Cell cell = new Cell(random,random2,'N');
        cell.addCellToGrid(grid, random, random2, cell.type);
    }
printGrid(grid);

}

public static void main(String[] args)
{
    Setup e = new Setup();
    e.start(10);
}

}

当我运行时,我没有任何字符输出。我的假设是它只是打印一个空的50x50阵列。我不确定我在这里做错了什么。我如何创建单元格并将它们添加到网格中有什么问题,还是我打印网格的问题?我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

根据我对您的问题和疑问的理解,您需要进行以下修改才能填充50 x 50阵列:

  1. 在for循环中移动random和random2语句
  2. 将您的随机内容从50 + 1更改为49 + 1,或者将您的数组大小更改为51 x 51
  3. 问题1 ,您必须在for循环中移动随机语句的原因是允许为您声明的10个单元格生成随机位置。作为循环外部意味着,您覆盖前一个单元格,因此您的2D数组将只有一个元素,因为您继续将单元格添加到网格的相同位置。

    //move the random and random1 inside for loop for random cell positions
    for (int i = 1; i < steps; i++) {
        int random = (int) (Math.random() * 49 + 1);
        int random2 = (int) (Math.random() * 49 + 1);
    
        Cell cell = new Cell(random, random2, 'N');
        cell.addCellToGrid(grid, random, random2, cell.type);
    }
    

    问题2 导致漏洞的编号为50,如果您尝试使用该编号访问网格数组,则可以保证{问题} ArrayIndexOutOfBoundException

    int random = (int) (Math.random() * 49 + 1);
    int random2 = (int) (Math.random() * 49 + 1);
    
    //this can cause ArrayIndexOutOfBoundException, either change to above or 
    //modify the dimension of your grid array
    int random2 = (int) (Math.random() * 50 + 1);
    

    你知道Arrays.deepToString()吗?也许不是,您可以使用它来打印您的2D数组而不是有两个for循环,如下所示:

    Arrays.deepToString(grid); 
    

    如果您进行了以上更改,请运行您的程序。保证在网格阵列的10个随机位置有10个单元。

    我添加了另一种方法,只打印单元格的有效位置,运行代码后,得到以下结果:

    grid[9][12]
    grid[10][26]
    grid[16][17]
    grid[19][17]
    grid[26][34]
    grid[27][13]
    grid[39][18]
    grid[46][41]
    grid[49][22]
    

    最后但同样重要的是,我很好奇当你的随机数生成器返回已经有单元格的位置时会发生什么。您将覆盖该单元格。我认为您需要检查以确保random数组中生成的random1grid内部没有单元格。如果该位置有一个有效的Cell,那么您需要生成另一个位置,将该单元存储在该位置。我会把它留给你处理。

    要在一行中打印每一行,您可以将打印方法更改为单循环,然后打印grid [i],其中i是循环计数器,从0到小于50.参见下文:

    for(int i = 0; i < 50; i++) {
         System.out.println(Arrays.toString(grid[i]));
     }
    

    每个网格[i]是一列。我们使用println()在一行中打印每一行。