Download presentation
Presentation is loading. Please wait.
Published byCharles Miller Modified over 9 years ago
1
CMSC 150 MULTI-DIMENSIONAL ARRAYS CS 150: Mon 27 Feb 2012
2
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
3
Motivation
4
pixel
5
2D Array can be primitive or class type S yntax: 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];
6
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 null
7
In Memory 0x12AB79 [0][0] [0][1] [0][2] 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 [1][0] [1][1] [1][2]
8
In Memory 0x12AB79 [0][0] [0][1] [0][2] 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 [1][0] [1][1] [1][2] row 0 row 1
9
In Memory 0x12AB79 77 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
10
In Memory 0x12AB79 77 42 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
11
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2]
12
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 154
13
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 154 84
14
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 154 84 202
15
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 154 84 202 myArray[0] myArray is an int[][] myArray[0] is an int[] myArray[0][0] is an int myArray is an int[][] myArray[0] is an int[] myArray[0][0] is an int
16
In Memory 0x12AB79 77 42 101 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 [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 154 84 202 myArray[1] myArray is an int[][] myArray[1] is an int[] myArray[1][0] is an int myArray is an int[][] myArray[1] is an int[] myArray[1][0] is an int
17
In Memory 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { String[][] strArray; strArray = new String[2][3]; for (int i = 0; i < strArray.length; i++) { for (int j = 0; j < strArray[i].length; j++) { strArray[i][j] = new String((i+1) + “ and “); } strArray [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 0x33DB26 0x33DC01 0x33DD25 0x33DE04 0x33DE29 0x33DF22
18
In Memory 0x12AB79 public class 2DArrayExample { public static void main(String[] args) { String[][] strArray; strArray = new String[2][3]; for (int i = 0; i < strArray.length; i++) { for (int j = 0; j < strArray[i].length; j++) { strArray[i][j] = new String((i+1) + “ and “); } strArray [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] 0x33DB26 0x33DC01 0x33DD25 0x33DE04 0x33DE29 0x33DF22 # rows # cols
19
Managing 2D Arrays Typically use nested for loops: int[][] randomNumbers = int[1024][2048]; for (int i = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { randomNumbers[i][j] = generator.nextInt(10000); }
20
Managing 2D Arrays Typically use nested for loops: int[][] randomNumbers = int[1024][2048]; for (int i = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { randomNumbers[i][j] = generator.nextInt(10000); } int numberOfOdds = 0; for (int i = 0; i < randomNumbers.length; i++) { for (int j = 0; j < randomNumbers[i].length; j++) { if ( randomNumbers[i][j] % 2 != 0 ) { numberOfOdds++; } }
21
Let’s Try Some… Photo by Gary Mueller, from www.allaboutbirds.orgwww.allaboutbirds.org (Cornell Laboratory of Ornithology)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.