找不到任何名字

时间:2013-12-02 17:43:23

标签: arrays 2d cell processing

这是我第一次遇到错误,所以我不明白这意味着什么。这段代码已经从在线教程改编而来,这使它更加令人困惑。在第14行,我收到错误消息“找不到任何名为'innerGrid'的东西”。这指的是什么?对我来说,代码已经很好地定义了innerGrid。

    //2d array
innerGrid [][] cell;

//number of columns
int col = 50;
//number of rows
int row = 10;

void setup()
{
  size(500, 100);
  background(0);
  //layout grid
  innerGrid= new cell[col][row]; //cannot find anything named 'innerGrid'
  //write columns (int i) and row numbers (int j) in counters
  for (int i = 0; i < col; i ++) 
  {
    for (int j = 0; j < row; i ++) 
    {
      innerGrid[i][j] = new innerGrid(i*10, j*10, 10, i+j);
    }
  }
  //create base layer
  initialGrid();
}

void initialGrid()
{
 for (int i = 0; i < cols; i ++ ) 
 {    
    for (int j = 0; j < rows; j ++ ) 
    {
      innerGrid[i][j].display();
    }
  } 
}

void draw()
{
 for (int i = 0; i < cols; i ++ ) 
 {    
    for (int j = 0; j < rows; j ++ ) 
    {
      innerGrid[i][j].updateColor();
    }
  } 
}

1 个答案:

答案 0 :(得分:1)

这段代码中的事情很混乱......

声明一个你需要声明它的类型的变量,innerGrid不是一个类型,除非有一个叫做this的类(按惯例的类应该有一个大写的第一个字母)。看起来你正在寻找的是一个双重维数的整数

int[][] innerGrid; // int is the type and innerGrid is the name of the var (array)
innerGrid = new int [dim1][dim2]// this also can be done in same line above...

但是你会发现很多其他问题,因为类单元格不存在......在同一行中

= new cell[][]

没有意义,除非有一个类'单元'(没有大写......哼哼)不能说。在这种情况下,innerGrid数组应该保持单元格的实例,并声明为此

cell[][] innerGrid = new cell [rows][cols];

基本用法如下:

// declare a 2D array of ints
int[][] intArray = new int [10][20];

// declare a 2D array of Cell
Cell[][] cellArray = new Cell [10][20]; // note we didn't create any Celll yet
                                        //only an array to hold them
void setup(){
for (int i = 0; i < intArray.length; i ++) {
  for (int j = 0; j < intArray[0].length ; j ++) {
    intArray[i][j] = i+j;
    cellArray[i][j] = new Cell();// here we creates the cells objects;
  }
} 

println("\n\n intArray: \n");
for (int i = 0; i < intArray.length; i ++) {
  for (int j = 0; j < intArray[0].length ; j ++) {
    println(" intArray: ["+i+"]["+j+"] = " +  intArray[i][j]);
  }
} 

println("\n\n cellArray: \n");
for (int i = 0; i < intArray.length; i ++) {
  for (int j = 0; j < intArray[0].length ; j ++) {
    println("cellArray: ["+i+"]["+j+"] = " +  cellArray[i][j]);
  }
} 
}




class Cell {
  float x;
  // a constructor
  Cell () {
    x = random(1);
  }

  String toString(){
    return nf(x, 2, 5);
  }
}