0
我想創建一個包含數組的ArrayList,並調用該數組內的對象的函數。如何在ArrayList中調用Array的函數? Java
我想調用Array中的函數display(),但即使數組包含一個對象,我也得到一個NPE。
這是我的代碼:
class Ball
{
int x;
int y;
int size;
color c;
Ball()
{
x = int (random(width));
y = int (random(height));
size = int (random(100));
c = color(random(255));
}
void display()
{
fill(c);
ellipse(x,y,size,size);
}
}
ArrayList<Ball[]> balls;
void setup()
{
size(500,500);
balls = new ArrayList<Ball[]>();
for(int i = 0; i < 1; i++)
{
balls.add(new Ball[2]);
println(balls);
}
}
void draw()
{
background(255);
for(int i = 0; i < 1; i++)
{
Ball[] b = balls.get(i);
b[i].display();
}
}
沒有任何人知道如何做到這一點?
'balls.get(0)[0]。顯示()'會的工作 – 11thdimension
你的問題是,你沒有創造任何球 - 只是一個空數組。 –
該數組爲空。你沒有創造任何球。 –