3/22/2012 One can declare an array of any type Review int [] myInts; • Recursion int myInt; float [] myFloats; float myFloat; • Factorial (Iterative and Recursive versions) String [] myStrs; String myStr; • Call Stack (Last-in, first-out Queue) … just add [] • Tracing recursive functions • Fibonacci Sequence – Recursive Implementation To create and size the array, use the new keyword • Recursive Maze Generation myInts = new int[10]; myFloats = new float[20] myStrs = new String[30]; One can declare an array of custom classes If this is a float… float myFloat; Mammoth[] mammoths; // declare array variable void setup() { and this is an array of floats… mammoths = new Mammoth[30]; // create + size array } float[] myFloats; class Mammoth { String name; String sound; what is this? Mammoth( String name, String sound ) { float[][] myFloats2; this.name = name; this.sound = sound; } } Declare, size, and fill a 2D array float[][] vals; void setup() { void setup() { vals = new float[20][300]; float[][] myFloats2 = new float[10][10]; for (int i=0; i<20; i++) { for (int i=0; i<10; i++) { println( vals[i].length ); // What is going on here? for (int j=0; j<10; j++) { } myFloats2[i][j] = random(100); } } } j 300 0 1 2 3 4 5 6 7 8 9 300 i } 0 300 1 2 300 3 300 4 300 5 6 7 8 9 ex1.pde ex2.pde 1
3/22/2012 “Ragged” Arrays Cellular Automata A regular grid float[][] ragged; of Cells ragged 0 1 2 void setup() { 0 Cell 1.23 3.25 9.84 ragged = new float[5][]; 0 1 2 3 4 Two States 1 8.87 6.70 5.10 0.59 4.44 1. On for (int i=0; i<5; i++) { 2. Off int n = int(random(10)); 0 1 ragged[i] = new float[n]; 2 9.01 4.98 } Neighborhood 0 for (int i=0; i<5; i++) { 3 8.50 Cell states println(ragged[i].length); evolve over } 1 2 3 0 time according 4 4.79 8.11 0.98 1.87 } to a predefined set of rules. ex3.pde Sample Set of Rules – Conway's Game of Life Interesting Patterns – Conway's Game of Life 1. Any live cell with fewer than two live neighbors dies, as if caused by under-population. 2. Any live cell with two or three live neighbors lives on to the next generation. 3. Any live cell with more than three live neighbors dies, as if by overcrowding. 4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. An example of "Emergence" http://en.wikipedia.org/wiki/Conway%27s_game_of_life http://en.wikipedia.org/wiki/Conway%27s_game_of_life current int N = 5; boolean[] cell = new boolean[N]; next cell � One-dimensional array false 0 false 1 false 2 Top-level procedure false 3 1. Draw the current grid false 4 2. Advance game by applying rules to all cells of current and filling next 3. Swap current and next grid 2
3/22/2012 int N = 5; int N = 5; boolean[][] cell = new boolean[N][N]; boolean[][] cell = new boolean[N][N]; cell[1][2] = true; cell 0 1 2 3 4 � Two-dimensional array cell 1 2 3 4 0 0 false false false false false 0 1 2 3 4 false false false false false 0 … an array of arrays 1 false false false false false 0 1 2 3 4 false false true false false 1 2 false false false false false 0 1 2 3 4 false false false false false 2 3 false false false false false 1 2 3 4 0 false false false false false 3 4 false false false false false false false false false false 4 current: cell[r][c][0] next: cell[r][c][1] // 3-Dimensional Array int N = 50; boolean[][][] cell = new boolean[N][N][2]; cell[1][2][0] = true; 3
Recommend
More recommend