Download presentation
Presentation is loading. Please wait.
1
Lecture 6 2d Arrays Richard Gesick
2
2d arrays Working with 2D arrays is similar to 1D arrays. But realize you need to specify two dimensional sizes and any time you access an element, you need to specify both indices.
3
define a 2D array <type>[ , ]<name> = new<type>[<size_1>,<size_2>]; For example: int[ , ] grid = new int [10,20];
4
Working with 2D arrays usually involves embedded loops like this
for(int col=0; col < 10; col++) for(int row=0; row < 20; row++) grid[col,row] = col * row;
5
What is generated? for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) Console.Write((i * j) + " " ); } Console.WriteLine();
6
What is generated? int[,] grid = new int[10, 20];
for (int col = 0; col< 10;col++) for (int row = 0; row < 20; row++) grid[col, row] = col * row;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.