Download presentation
Presentation is loading. Please wait.
Published byOswin Scott Modified over 9 years ago
1
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java
2
Array dimensions Arrays we have studied so far can be arranged in a line In this respect they could be thought of as “one dimensional” In Java, it is possible to have arrays of more than one dimension. 01234567
3
Two dimensional arrays A 2-dimensional array is sometimes called a ______ Instead of one _____, each element has two –First could be though of as a “___” –Second could be thought of as a “______” 0123 0 1 2 3
4
Using two-dimensional arrays Identify by placing two sets of open and close square brackets ([][]) after the ____ –Recall that an array type has one set of open and close square brackets –When you declare, put the number of _____and________ in between the brackets after the type that follows new: int[][] myMatrix = new int[5][7]; Identify an array element by placing the two indexes each between its own ______________________: –As always, columns and rows start numbering at zero –This would print the element in the 5 th row and 4 th column: System.out.print(myMatrix[4][3]);
5
How two dimensional arrays are stored Two dimensional arrays are stored as an “_______________” In the following example: int[][] myMatrix = new int[4][6]; myMatrix[2] would be a one dimensional array of length 6. myMatrix[2][0] would be the first element in that array –i.e. the first element in the 3 rd row of the matrix
6
Initializing two dimensional arrays Can initialize a two dimensional array –Put each row in ________ –Separate elements and rows by ______ –Put brackets around ________ Example: int[][] mySquare = {{8,1,6}, {3,5,7}, {4,9,2}};
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.