210 likes | 296 Views
CMSC 150 multi-dimensional Arrays. CS 150: Mon 27 Feb 2012 . Motivation. A digital image is a two-dimensional collection of pixels (picture elements) Each pixel has an associated color (green, brown, pink, yellow) Taken collectively, the pixels form the image. Motivation. Motivation. pixel.
E N D
CMSC 150multi-dimensional Arrays CS 150: Mon 27 Feb 2012
Motivation • A digital image is a two-dimensional collection of pixels (picture elements) • Each pixel has an associated color (green, brown, pink, yellow) • Taken collectively, the pixels form the image
Motivation pixel
2D Array • can be primitive or class type • Syntax: • type[][] variableName; • type[][]variableName = new type[numRows][numCols]; • Examples: Color[][] pixels = new Color[100][256]; boolean[][] missingTeeth = new boolean[2][16]; String[][] firstLastNames = new String[NUM_STUDENTS][2];
In Memory myArray null public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } }
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] row 0 [0][1] [0][2] [1][0] [1][1] row 1 [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] [0][2] [1][0] [1][1] [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] 42 [0][2] [1][0] [1][1] [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] 42 [0][2] 101 [1][0] [1][1] [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] 42 [0][2] 101 [1][0] 154 [1][1] [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] 42 [0][2] 101 [1][0] 154 [1][1] 84 [1][2]
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 [0][1] 42 [0][2] 101 [1][0] 154 [1][1] 84 [1][2] 202
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } [0][0] 77 myArray[0] [0][1] 42 [0][2] 101 myArray is an int[][] myArray[0] is an int[] myArray[0][0] is an int [1][0] 154 [1][1] 84 [1][2] 202
In Memory myArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { int[][] myArray; myArray = new int[2][3]; myArray[0][0] = 77; myArray[0][1] = 42; myArray[0][2] = 101; myArray[1][0] = 77 + 77; myArray[1][1] = 42 + 42; myArray[1][2] = 101 + 101; } } myArray is an int[][] myArray[1] is an int[] myArray[1][0] is an int [0][0] 77 [0][1] 42 [0][2] 101 [1][0] 154 [1][1] 84 myArray[1] [1][2] 202
In Memory strArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { String[][] strArray; strArray = new String[2][3]; for (inti = 0; i < strArray.length; i++) { for (int j = 0; j < strArray[i].length; j++) { strArray[i][j] = new String((i+1) + “ and “); } } } [0][0] 0x33DB26 0x33DC01 [0][1] [0][2] 0x33DD25 [1][0] 0x33DE04 [1][1] 0x33DE29 [1][2] 0x33DF22
In Memory strArray 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { String[][] strArray; strArray = new String[2][3]; for (inti = 0; i < strArray.length; i++) { for (int j = 0; j < strArray[i].length; j++) { strArray[i][j] = new String((i+1) + “ and “); } } } [0][0] 0x33DB26 # rows 0x33DC01 [0][1] [0][2] 0x33DD25 [1][0] 0x33DE04 [1][1] 0x33DE29 # cols [1][2] 0x33DF22
Managing 2D Arrays • Typically use nested for loops: int[][] randomNumbers = int[1024][2048]; for (inti = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { randomNumbers[i][j] = generator.nextInt(10000); } }
Managing 2D Arrays • Typically use nested for loops: int[][] randomNumbers = int[1024][2048]; for (inti = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { randomNumbers[i][j] = generator.nextInt(10000); } } intnumberOfOdds = 0; for (inti = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { if ( randomNumbers[i][j] % 2 != 0 ) { numberOfOdds++; } } }
Let’s Try Some… Photo by Gary Mueller, from www.allaboutbirds.org (Cornell Laboratory of Ornithology)