Hi,
I have a simple question about enhanced for loops.
I created a new arraylist and filled them with objects. and now I am trying to select every nth object. I tried to use:
if(arraylist.size() % 2 == 0) {}
but it doesn’t work. What should I do for selecting every nth object in that arraylist, inside an enhanced for loop?
cells = new ArrayList <Cell>();
createCells(cellSize);
void setup() {
size(1920, 960);
rectMode(CENTER);
cells = new ArrayList <Cell>();
createCells(cellSize);
}
void draw() {
for ( Cell c : cells) {
if (cells.size() % 2 == 0) {
// do struff here
}
}
}
void createCells (float size) {
for ( int i = 0; i<height; i++) {
for (int j = 0; j<width; j++) {
if ( i% size == 0) {
if (j% size == 0) {
float cellOpacity = random(20);
float cellRotation = random(360);
cells.add(new Cell( j, i, size, cellOpacity, cellRotation));
}
}
}
}
}
class Cell {
float x, y, scale;
float opacity;
float rotation;
Cell( float x, float y, float scale, float opacity, float rotation) {
this.x = x;
this.y = y;
this.scale = scale;
this.opacity = opacity;
this.rotation = rotation;
}
}