Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 2D Arrays Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 14 2D Arrays Richard Gesick."— Presentation transcript:

1 Lecture 14 2D Arrays Richard Gesick

2 2D vs 1D Arrays Still hold several values of the same type (homogeneous) Still based on a slot number (the index number), but has two now Still have instant access Still Static

3 What 2D arrays look like . . . . . . 1 2 3 4 5 6 myArray 1 2 3 4 5 6
1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] . . . 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] . . .

4 Creating 2D Arrays Notice The comma The columns and rows values
<data type>[,] <name> = new <data type> [<rows>,<cols>]; Notice The comma The columns and rows values

5 Examples An array of shorts: An array of floats: An array of booleans:
short[,] someArray = new short[50,5]; An array of floats: float[,] myArray = new float[25,10]; An array of booleans: bool[,] list = new bool[640,480]; An array of chars: char[,] characters = new char[2,2];

6 Modifying a 2D Array myArray[2,3] = 42 1 2 3 4 5 6 myArray 1 2 3 4 5 6
1 2 3 4 5 6 [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] myArray 1 [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] 2 [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] 3 [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] 4 [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] 5 [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] 6 [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] myArray[2,3] = 42

7 Iterating Through 2D Arrays
Working with 2D arrays, you will use two loops (nested) Example: byte[,] myPic = new byte[480,640]; for (int x=0; x < 480; x++) { for (int y=0; y < 640; y++) { myPic[x,y] = 0; }

8 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; }

9 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } myPic 1 2 1 2

10 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic x 1 2

11 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic x 1 2

12 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic x 1 y 2

13 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic x 1 y 2

14 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic x What happens? 1 y 2

15 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 x 1 y 2

16 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 x 1 1 y 2

17 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 x 1 1 2 y

18 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 x 1 1 y 2

19 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } myPic 1 2 1 1 x 1 2 y 2

20 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 x 1 2 y 2

21 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 x 1 2 y 2

22 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 x 1 2 y 2

23 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 x 1 3 y 2

24 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 x 1 3 y 2

25 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 2

26 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 2

27 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 y 2

28 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 y 2

29 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 1 y 2

30 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 x 1 1 1 y 2

31 Now skipping ahead a bit

32 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 2 1 1 1 1 x 2 1 1 1 y

33 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 2 x 1 1 1 1 2 2 1 1 y

34 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 2 x 1 1 1 1 2 2 1 1 y

35 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 2 x 1 1 1 1 2 2 1 1 1 y

36 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 1 1 1 2 x 2 1 1 1 3 y

37 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 1 1 1 2 x 2 1 1 1 3 y

38 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 1 1 1 1 3 x 2 1 1 1

39 Line by Line (a smaller example)
byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 3 1 1 1 1 x 2 1 1 1

40 Done! byte[,] myPic = new byte[3,3]; for (int x=0; x < 3; x++) { for (int y=0; y < 3; y++) { myPic[x,y] = 1; } 1 2 myPic 1 1 1 . . 1 1 1 1 . 2 1 1 1

41 Summary 2D Arrays are similar to 1D arrays
Usually requires nested loops


Download ppt "Lecture 14 2D Arrays Richard Gesick."

Similar presentations


Ads by Google