令牌“]”上的语法错误,此令牌后预期的VariableDeclaratorld

时间:2013-06-23 02:26:35

标签: java arrays syntax processing

我试图将thistime []传递给类并使用它来定义两个rects的宽度和高度。这只是我初始代码的简化版本,我在令牌“]”上得到了错误的语法错误,VariableDeclaratorld预计在这个标记“,这是我的代码:

ArrayList textlines;

int xpos=20;
int ypos=20;
int[]thistime = new int[2];

void setup() {
  size(1200, 768);
  textlines = new ArrayList();
  thistime[0] =3;
  thistime[1] =30;
}

void draw() {
}


void mousePressed() {
  textlines.add(new Line(xpos, ypos,thistime));
  for (int i=0; i<textlines.size(); i++) {

    Line p=(Line)textlines.get(i);
    p.display();
  }
}


class Line {

  int x;
  int y;
  int thatimee[];

  Line(int xpo, int ypo, int thetimee[]) {
    x =xpo;
    y =ypo;
    thatimee[]= new int[thetimee.length];
    thatimee[0]=thetimee[0];
    thatimee[1]=thetimee[1];
  }

  void display() {
    fill(50, 50, 50);
    rect(random(width), random(height), thatimee[0],thatimee[0] );
    rect(random(width), random(height), thatimee[1], thatimee[1]);
  }
}

错误在行

thatimee[]= new int[thetimee.length];

谁知道原因?

4 个答案:

答案 0 :(得分:2)

尝试删除作业中的[]。像这样:

thattimee = new int[thetimee.length];

答案 1 :(得分:2)

只需使用

thatimee = new int[thetimee.length];

[]用于声明数组。初始化时不应使用它。

答案 2 :(得分:2)

初始化阵列时无法放置thatimee[]。你只需输入:

thatimee = new int[thetimee.length];

thatimee表示数组的句柄,并且您正在句柄中存储内容。

答案 3 :(得分:2)

Line(int xpo, int ypo, int thetimee[]) {
    x = xpo;
    y = ypo;
    thatimee = new int[thetimee.length];
    thatimee[0] = thetimee[0];
    thatimee[1] = thetimee[1]; 
}

您已将变量“thatimee”声明为数组,在初始化变量时删除Line范围内的“[]”。