Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 2d Arrays CSE 1322 4/26/2018.

Similar presentations


Presentation on theme: "Lecture 4 2d Arrays CSE 1322 4/26/2018."— Presentation transcript:

1 Lecture 4 2d Arrays CSE 1322 4/26/2018

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. 4/26/2018

3 CREATE array nums [size_1][size_2]
define a 2D array CREATE array nums [size_1][size_2] 4/26/2018

4 C# define a 2D array <type>[ , ]<name> = new<type>[<size_1>,<size_2>]; For example: int[ , ] grid = new int [10,20]; 4/26/2018

5 Java define a 2D array <type>[ , ]<name> = new<type>[<size_1>][<size_2>]; For example: int[ ][ ] grid = new int [10][20]; 4/26/2018

6 Working with 2D arrays usually involves nested loops like this using a array called grid: FOR each element in a row FOR each element in a column     grid[row][col] = col * row; 4/26/2018

7 Working with 2D arrays usually involves nested loops like this in java: for(int row=0; row < 10; row++)   for(int col=0; col < 20; col++)     grid[row][col] = col * row; 4/26/2018

8 Working with 2D arrays usually involves nested loops like this in C#:
for(int row=0; row < 10; row++)   for(int col=0; col < 20; col++)     grid[row,col] = col * row; 4/26/2018

9 What is generated? Java: C#
int[,] grid = new int[10, 20]; for (int r = 0; r< 10;r++) for (int c = 0; c < 20; c++) grid[r, c] = c* r; int[][] grid = new int[10][20]; for (int r = 0; r< 10;r++) for (int c = 0; c < 20; c++) grid[r][c] = c* r; 4/26/2018

10 multi-dimension arrays
CREATE array nums [size_1][size_2][size_3] Use nested loops, one for each dimension to traverse the array FOR each number in the row FOR each number in the column FOR each number in the third dimension do some processing ENDFOR 4/26/2018

11 C# multi-dimension arrays
int[,,] nums= new int [5,6,7]; 3 nested loops using GetLength(0), GetLength(1) and GetLength(2) for(int i=0; i<nums.GetLength(0);i++) { for (int j=0; j< nums.GetLength(1);j++) for(int k=0; k<nums.GetLength(2);k++) // do some processing } 4/26/2018

12 C# multi-dimension arrays
int[,,] nums= new int [5,6,7]; Or use a foreach loop as long as you aren’t changing the contents of the array foreach ( int i in nums) {. . .} public static int findLargest(int[,,]n) { int max = n[0,0,0]; foreach (int j in n) { if( j > max) max = j; } return max; } 4/26/2018

13 Java multi-dimension arrays
int[][][] nums= new int [5][6][7]; 3 nested loops using nums.length, nums[0].length and nums[0][0].length for(int i=0; i<nums.length;i++) { for (int j=0; j< nums[0].length;j++) for(int k=0; k<nums[0][0].length;k++) // do some processing } 4/26/2018

14 Java multi-dimension arrays
int[][][] nums= new int [5][6][7]; Use the enhanced for loop/for each loop on each subarray structure, as long as you aren’t changing the contents of the array public static int findLargest3(int[][][]n) { int max = n[0][0][0]; for ( int [][] e : n ) for ( int [] d : e ) for ( int i : d ) { if( i > max) max = i; } return max; } 4/26/2018


Download ppt "Lecture 4 2d Arrays CSE 1322 4/26/2018."

Similar presentations


Ads by Google