Programming types always get excited/angry about pointers. I’m sure they’re important, but I don’t really know what a pointer is. However, this strikes me as quite a nice description, by Daniel Shiffman, from the Learning Processing book:

9.2 What is an array? From Chapter 4, you may recall that a variable is a named pointer to a location in memory where data is stored. In other words, variables allow programs to keep track of information over a period of time. An array is exactly the same, only instead of pointing to one singular piece of information, an array points to multiple pieces.

Declaring Arrays

With variables, you’d use something like this:

int myIntegerVariable;

With arrays it looks like:

int [] myArrayOfIntegers = new int [23];

Or for an array of objects of a class (e.g. Bubble) you’ve defined:

Bubble[] myBubbles = new Bubble[23];

So the differences here are:

  1. Use the [] notation to specify that you’re creating an array
  2. Create the array, as well as declaring it, by using the new notation (Is it possible or useful to just declare an array without creating an instance of it, with int [] myArrayOfIntegers;? I don’t know…
  3. Give it a predetermined size (of 23 integers or Bubbles in the examples above)

Length of Arrays

Useful to know, when filling the array, or when avoiding overstepping its bounds. Here’s the notation in a filling loop:

for (int i = 0; i < myArrayOfIntegers.length; i++) { 
  myArrayOfIntegers[i] = 0;
}

Sketch: arrays_1.pde

So here, I’m just trying to create an array of objects (“grains”, which in this simple test are just filled circles), that populate the screen from the top left corner, filling up a row until they get to the bottom of the screen, then starting a new column, filling that up, and so on.

Eventually I hope to do something more interesting with this.

My class definition is simple:

class Grain { 
  // data
  float xPos;
  float yPos;
  float xWidth;
  float yHeight;
  color col;  

  // constructor, defined with arguments
  Grain (float xPos_, float yPos_) {
    xPos = xPos_;
    yPos = yPos_; 
    xWidth = 9;
    yHeight = 9;
    col = color(0); 
  }  
  
  // functions
  void display() {
    ellipseMode(CENTER);
    noStroke();
    fill(col);
    ellipse(xPos, yPos, xWidth, yHeight); 
  }
}

Where it gets tricky is in the logic I have to use in the setup() function to create and fill the array:

void setup() {
  size(400,400);

  for (int i = 0; i < myGrains.length; i=i+1) {
    float yGridPosition; // where is each grain in the grid: y axis
    float xGridPosition; // where is each grain in the grid: x axis

    yGridPosition = (currentRow*20);
    xGridPosition = currentColumn*20;
    
    if (yGridPosition > (height-40)) {
      // println("end of column");
      currentColumn = currentColumn + 1;
      currentRow = 0;
    }  
     
    myGrains[i] = new Grain(xGridPosition, yGridPosition); // Initialize objects 
    currentRow = currentRow + 1;
  }  
  
}

What I’m finding tricky is why I have to do this here in setup. Ultimately all this jostling for position in the grid just comes down to x and y position, which is what the object needs to know in oder to be drawn, but it feels like this isn’t modular enough. At the very least, I’d like to be able to redraw these objects with each screen refresh to animate them in some way…