home/ atoms/ processing-arrays

Arrays in Processing store multiple values under one name, accessed by zero-based index

An array is a fixed-size ordered list where every element has the same type. Arrays are declared with [] and created with new: float[] positions = new float[10]. The first element is at index [0], the last at [length-1]. Array.length gives the number of elements. Arrays are essential for managing multiple instances of the same kind of data (positions, speeds, colors). Arrays of objects enable patterns like particle systems. A common error is accessing index >= length (ArrayIndexOutOfBoundsException). The arraycopy() function is the fastest way to copy between arrays.

Examples

float[] x = new float[5]; for (int i = 0; i < x.length; i++) { x[i] = i * 20; ellipse(x[i], 50, 10, 10); }

Assessment

Write a sketch that stores the last 50 mouseY values in an array and draws a line graph of those values across the canvas width; explain why the array must be declared outside draw().

“Arrays can make the task of programming much easier.”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 64