CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick
CSE D 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
CSE 1301 What 2D arrays look like myArray [6,0] [6,1][6,2][6,3][6,4][6,5][6,6] [5,0] [5,1][5,2][5,3][5,4][5,5][5,6] [4,0] [4,1][4,2][4,3][4,4][4,5][4,6] [3,0] [3,1][3,2][3,3][3,4][3,5][3,6] [2,0] [2,1][2,2][2,3][2,4][2,5][2,6] [1,0] [1,1][1,2][1,3][1,4][1,5][1,6] [0,0] [0,1][0,2][0,3][0,4][0,5][0,6]......
CSE 1301 Creating 2D Arrays [,] = new [, ]; Notice – The comma – The columns and rows values
CSE 1301 Examples An array of shorts: 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];
CSE 1301 Modifying a 2D Array myArray [6,0] [6,1][6,2][6,3][6,4][6,5][6,6] [5,0] [5,1][5,2][5,3][5,4][5,5][5,6] [4,0] [4,1][4,2][4,3][4,4][4,5][4,6] [3,0] [3,1][3,2][3,3][3,4][3,5][3,6] [2,0] [2,1][2,2][2,3][2,4][2,5][2,6] [1,0] [1,1][1,2][1,3][1,4][1,5][1,6] [0,0] [0,1][0,2][0,3][0,4][0,5][0,6] myArray[2,3] = 42
CSE 1301 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; }
CSE 1301 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; }
CSE 1301 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
CSE 1301 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 0 x
CSE 1301 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 0 x
CSE 1301 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 0 x 0 y
CSE 1301 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 0 x 0 y
CSE 1301 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 0 x 0 y What happens?
CSE 1301 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 0 x 0 y
CSE 1301 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 0 x 1 y
CSE 1301 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 0 x 1 y
CSE 1301 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 0 x 1 y
CSE 1301 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 0 x 2 y
CSE 1301 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 0 x 2 y
CSE 1301 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 0 x 2 y
CSE 1301 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 0 x 2 y
CSE 1301 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 0 x 3 y
CSE 1301 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 0 x 3 y
CSE 1301 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 x
CSE 1301 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 x
CSE 1301 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 x 0 y
CSE 1301 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 x 0 y
CSE 1301 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 x 0 y
CSE 1301 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 x 1 y
CSE 1301 Now skipping ahead a bit
CSE 1301 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 2 x 1 y
CSE 1301 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 2 x 2 y
CSE 1301 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 2 x 2 y
CSE 1301 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 2 x 2 y
CSE 1301 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 2 x 3 y
CSE 1301 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 2 x 3 y
CSE 1301 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 3 x
CSE 1301 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 3 x
CSE 1301 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; } myPic...
CSE 1301 Summary 2D Arrays are similar to 1D arrays Usually requires nested loops