2013-06-23 275 views
0

我試圖將this []傳遞給類並使用它來定義兩個rects的寬度和高度。這只是我初始代碼的簡化版本,而我得到令牌 「]」 的errorsyntax錯誤,此令牌」後VariableDeclaratorld預期,這裏是我的代碼:?令牌上的語法錯誤「]」,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]; 

誰知道原因

回答

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範圍內的「[]」。

2

嘗試刪除作業中的[]。像這樣:

thattimee = new int[thetimee.length]; 
2

只是使用

thatimee = new int[thetimee.length]; 

的[]是用於聲明的陣列。初始化時不應使用它。

2

初始化數組時,不能放thatimee[]。您只需把:

thatimee = new int[thetimee.length]; 

thatimee表示數組的手柄,並且要存儲在手柄的東西。

相關問題