Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 201 Tarik Booker California State University, Los Angeles.

Similar presentations


Presentation on theme: "CS 201 Tarik Booker California State University, Los Angeles."— Presentation transcript:

1 CS 201 Tarik Booker California State University, Los Angeles

2 What we will cover…  2D Array Basics  Obtaining the length of Two-Dimensional Arrays  Ragged Arrays  Processing Two-Dimensional Arrays  Passing Two-Dimensional Arrays to Methods  Multidimensional Arrays

3 Two-Dimensional Array Basics  So far, we’ve covered single dimensional arrays  Now we cover two (or more) dimension arrays We will start with 2 dimensions, and talk about more…

4 Two-Dimensional Array Basics (2)  Why 2D arrays? You may have information that is easier to think of in a 2D fashion ○ Grid ○ Plane ○ Board Tic Tac Toe 2D arrays are simply a more convenient way of visualizing information ○ 2D’s are equivalent to 1D’s

5 Declaring a Two-Dimensional Array Variable  Syntax for two-dimensional array variable: elementType[][] arrayRefVar; Ex: ○ int[][] matrix;  Creating the array matrix = new int[5][5];

6 Creating the array  Multidimensional array variables also use zero-based indexing Array of n by m elements 0n 0 m

7 Accessing the Array  To access a two-dimensional (or more) array, simply use put brackets around each element Ex:int[][] matrix = new int[5][5]; Accessing: ○ matrix[2][1] = 7; Sets the element at row 2, by column 1 to 7

8 Obtaining the Length of Arrays  A two-dimensional array is just a single dimensional array of other single dimensional arrays There is the length of total number of subarrays There is also the length of each subarray If a two-dimensional array has rows of different lengths, it is called a ragged array

9 Ragged Arrays  Arrays that have different row lengths Ex: int[][] triangleArray = { {1,2,3}, {2,3}, {3} }; This array has different row sizes

10 Ragged Arrays (2)  You can also create a ragged array with this syntax: int[][] triangleArray = new int[3][]; triangleArray[0] = new int[3]; triangleArray[1] = new int[2]; triangleArray[2] = new int[1];  You can assign values to the array: triangleArray[0][2] = 50; triangleArray[2][0] = 12;

11 Processing Two-Dimensional Arrays  We used a for loop to process single dimensional arrays  We will also now use nested for loops to process two-dimensional arrays Ex:Initializing arrays with random values for(int row=0; i<matrix.length; row++) { for(int column = 0; column < matrix[row].length; column++) { matrix[row][column] = Math.random() * 100; }

12 Printing Two-Dimensional Arrays  You can also use nested for loops to print out the contents of each array for(int row=0; i<matrix.length; row++) { for(int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column]+” “); }  What are some other things you can do? ○ Sum all elements ○ Random shuffling

13 Passing Two-Dimensional Arrays to Methods  You pass a two-dimensional array to method similarly to a single dimensional array Pass the reference to the array When defining the method, make sure you include the dimension of the array ○ Ex: sum method (p294) public static int sum(int[][] m){ … }

14 Examples  There are many examples in the book Nearest points Sudoku Etc… How would you use this?

15 Multidimensional Arrays  Arrays are not limited to 2 dimensions, You can declare arrays of 2,3,5,…,n dimensions They are handled just like 2-dimensional arrays ○ Just account for the extra dimension ○ There are no limits (but memory space)


Download ppt "CS 201 Tarik Booker California State University, Los Angeles."

Similar presentations


Ads by Google