Home

Video: 2d arrays

Notes

int[][] vals;

void setup()
{
  vals = new int[5][3];
  for(int i = 0; i < vals.length; i++)
  {
    for(int j = 0; j < vals[i].length; j++)
    {
      vals[i][j] = int(random(0, 5));
    }
  }
  
  size(500, 500);
  noLoop();
  background(0);
}

void draw()
{
  fill(255);
  textSize(20);
  
  // show a grid of values
  for(int i = 0; i < vals.length; i++)
  {
    for(int j = 0; j < vals[i].length; j++)
    {
      text(vals[i][j], j * 40 + 150, i * 40 + 150);
    }
  }
  
  // print a particular column
  for(int i = 0; i < vals.length; i++)
  {
    println(vals[i][1]);
  }
  
  // print a particular row
  for(int j = 0; j < vals[2].length; j++)
  {
    println(vals[2][j]);
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.