1 / 21

CMSC 150 multi-dimensional Arrays

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.

lahela
Download Presentation

CMSC 150 multi-dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150multi-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. Motivation pixel

  5. 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];

  6. 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; } }

  7. 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]

  8. 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]

  9. 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]

  10. 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]

  11. 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]

  12. 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]

  13. 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]

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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); } }

  20. 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++; } } }

  21. Let’s Try Some… Photo by Gary Mueller, from www.allaboutbirds.org (Cornell Laboratory of Ornithology)

More Related