cmsc 150 multi dimensional arrays
Download
Skip this Video
Download Presentation
CMSC 150 multi-dimensional Arrays

Loading in 2 Seconds...

play fullscreen
1 / 21

CMSC 150 multi-dimensional Arrays - PowerPoint PPT Presentation


  • 61 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CMSC 150 multi-dimensional Arrays' - lahela


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cmsc 150 multi dimensional arrays

CMSC 150multi-dimensional Arrays

CS 150: Mon 27 Feb 2012

motivation
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
2d array
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
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 memory1
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 memory2
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 memory3
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 memory4
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 memory5
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 memory6
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 memory7
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 memory8
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 memory9
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 memory10
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 memory11
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 memory12
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
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 arrays1
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
Let’s Try Some…

Photo by Gary Mueller, from www.allaboutbirds.org

(Cornell Laboratory of Ornithology)

ad