160 likes | 274 Vues
Learn the basics of 2D arrays, creating, modifying, and iterating through them with examples in different data types. Understand the structure, indexing, and usage of 2D arrays for efficient data manipulation. Dive into examples and practice to enhance your skills in working with 2D arrays.
E N D
2 Dimensional Arrays Briana B. Morrison CSE 1302C Spring 2010
Topics 2D vs 1D Creating 2D arrays Modifying Iterating Examples
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
What 2D arrays look like 1 2 3 4 5 6 0 0 [1,0] [2,0] [3,0] [4,0] [5,0] [6,0] [0,0] myArray 1 [1,1] [2,1] [3,1] [4,1] [5,1] [6,1] [0,1] 2 [1,2] [2,2] [3,2] [4,2] [5,2] [6,2] [0,2] . . . 3 [1,3] [2,3] [3,3] [4,3] [5,3] [6,3] [0,3] 4 [1,4] [2,4] [3,4] [4,4] [5,4] [6,4] [0,4] 5 [1,5] [2,5] [3,5] [4,5] [5,5] [6,5] [0,5] [1,6] [2,6] [3,6] [4,6] [5,6] [6,6] [0,6] 6 . . .
Creating 2D Arrays <data type>[,] <name> = new <data type> [<cols>,<rows>]; Notice The comma The columns and rows values
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];
Modifying a 2D Array 1 2 3 4 5 6 0 0 [1,0] [2,0] [3,0] [4,0] [5,0] [6,0] [0,0] myArray 1 [1,1] [2,1] [3,1] [4,1] [5,1] [6,1] [0,1] 2 [1,2] [2,2] [3,2] [4,2] [5,2] [6,2] [0,2] . . . 3 [1,3] [2,3] [3,3] [4,3] [5,3] [6,3] [0,3] 4 [1,4] [2,4] [3,4] [4,4] [5,4] [6,4] [0,4] 5 [1,5] [2,5] [3,5] [4,5] [5,5] [6,5] [0,5] [1,6] [2,6] [3,6] [4,6] [5,6] [6,6] [0,6] 6 myArray[2,3] = 42;
Iterating Through 2D Arrays Working with 2D arrays, you will use two loops (nested) Example: byte[,] myPic = newbyte[640,480]; for (int y=0; y < 480; y++) { for (int x=0; x < 640; x++) { myPic[x,y] = 0; } }
Example 1 class Program { static void Main(string[] args) { #region First example for (inti = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { Console.Write((i * j).ToString( ) + " " ); } Console.WriteLine(); } #endregion
Example 2 class Program { static int COLS = 10; static int ROWS = 10; static void Main(string[] args) { #region Second Example int[,] grid = new int[10, 20]; for (intcol = 0; col < 10; col++) for (int row = 0; row < 20; row++) grid[col, row] = col * row; PrintArray(grid); #endregion
Example 2 (cont.) private static void PrintArray(int[,] A) { for (intcol = 0; col < 10; col++) { for (int row = 0; row < 20; row++) { Console.Write(A[col, row] + " "); } Console.WriteLine(); } }
Battleship Example class Spot { public boolShipThere; public boolHasGuessed; } class Program { static int COLS = 10; static int ROWS = 10; static void Main(string[] args) { Spot[,] board = new Spot[COLS, ROWS]; for (inti = 0; i < COLS; i++) { for (int j = 0; j < ROWS; j++) { board[i, j] = new Spot(); } } PlaceShips(board);
Battleship Example private static void PlaceShips(Spot[,] board) { board[1, 1].ShipThere = true; board[1, 2].ShipThere = true; board[1, 3].ShipThere = true; board[1, 4].ShipThere = true; board[5, 5].ShipThere = true; board[6, 5].ShipThere = true; board[7, 5].ShipThere = true; board[8, 5].ShipThere = true; board[9, 5].ShipThere = true; }
Battleship Example char[ ] delimiters = { ' ' }; string input; do { PrintBoard(board); Console.Write("> "); input = Console.ReadLine(); if (input != "Q") { string[ ] tokens = input.Split(delimiters); int x = Int32.Parse(tokens[0]); int y = Int32.Parse(tokens[1]); board[x, y].HasGuessed = true; } } while (input != "Q"); }
Battleship Example private static void PrintBoard(Spot[,] board) { for (int row = 0; row < ROWS; row++) { for (intcol = 0; col < COLS; col++) { if (board[col, row].HasGuessed) { if (board[col, row].ShipThere) { Console.Write(" S "); } else { Console.Write(" . "); } } else { Console.Write(" - "); } } Console.WriteLine(); } }