The Coder's Handbook   

Arrays

WHAT IS AN ARRAY?

An array is a type of data structure used to store information.  Data structures are used to organize, arrange, and access collections of information.  


Arrays are the most basic data structures.  Take a moment to look at the diagram above.  Take note of the following:


CREATING AN ARRAY

When you want to create an array, you'll need to do three things:


Declaration


Initialize The Array



Initialize Each Element

Example

float[] numbers;


void setup()
{

   numbers = new float[200];


for(int i = 0; i < numbers.length; i++)

{

    numbers[i] = random(1, 10);

}

}




USING ARRAYS

Accessing an element

println(numbers[3]);

Set an element

numbers[3] = 74;

Get the length 

if(numbers.length < 5)

TWO DIMENSIONAL ARRAYS

Two dimensional arrays are not officially part of the course this year, but I've provided a quick summary here for students who want to explore them!

Rows and Columns

Syntax

Examples

final int ROWS = 100;

final int COLS = 50;

char [] grid = new char[ROWS][COLS]


for(int i = 0; i < COLS; i++)

{

  for(int j = 0; j < ROWS; j++)

  {

   grid[i][j] = "-";

  }

}


println(grid[32][45]); 

RESOURCES

Chapter 9 - Arrays (p163)

Arrays