arraylistclass.pde
来自「This is processing for java examples.」· PDE 代码 · 共 53 行
PDE
53 行
/** * ArrayList of objects * by Daniel Shiffman. * * This example demonstrates how to use a Java ArrayList to store * a variable number of objects. Items can be added and removed * from the ArrayList. * * Click the mouse to add bouncing balls. */ArrayList balls;int ballWidth = 48;void setup() { size(200, 200); smooth(); noStroke(); // Create an empty ArrayList balls = new ArrayList(); // Start by adding one element balls.add(new Ball(width/2, 0, ballWidth));}void draw() { background(255); // With an array, we say balls.length, with an ArrayList, we say balls.size() // The length of an ArrayList is dynamic // Notice how we are looping through the ArrayList backwards // This is because we are deleting elements from the list for (int i = balls.size()-1; i >= 0; i--) { // An ArrayList doesn't know what it is storing so we have to cast the object coming out Ball ball = (Ball) balls.get(i); ball.move(); ball.display(); if (ball.finished()) { // Items can be deleted with remove() balls.remove(i); } } }void mousePressed() { // A new ball object is added to the ArrayList (by default to the end) balls.add(new Ball(mouseX, mouseY, ballWidth));}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?