160 likes | 427 Views
Matrices. or 2-Dimensional Arrays or Arrays of Arrays. 0. 1. 2. 3. 4. 0. 1. 2. 3. 4. A. B. C. D. E. A. B. C. D. E. 0. 0. F. G. C. I. J. F. G. H. I. J. 1. 1. K. L. M. N. O. 2. 2. K. L. M. N. O. Definition.
E N D
Matrices or 2-Dimensional Arraysor Arrays of Arrays
0 1 2 3 4 0 1 2 3 4 A B C D E A B C D E 0 0 F G C I J F G H I J 1 1 K L M N O 2 2 K L M N O Definition • In programming, a matrix is an array of arrays; that is, a collection of collection of elements. Generally, we envision this as a grid (Model 1), but is more accurately represented by a collection of collections (Model 2) Model 1 Model 2
Places you may have heard of matrices • The awesome dot-matrix printers of the 1980s printed images and text by putting either a dot or no dot in each “space” of the paper. They were horrible quality because if you looked closely it was usually pretty easy to make out the individual dots. Image from Wikipedia
Places you may have heard of matrices • Matrices are all over the place in mathematics, with several branches of the subject (such as linear algebra) making extensive use of them. We will be coding a mathematical matrix later on for a project, and perform operations such as addition and subtraction on them. • In math, the number of columns in each row of a matrix must be the same; however, in Java arrays of arrays do not have this requirement. Image from Wikipedia
Places you may have heard of matrices • The use of the term “matrix” in the horrible 1999 movie The Matrixdoesn'treally correspond to how we use the term in this class, although I guess maybe the green grid patterns of characters used to represent code in the movie could be stored in arrays of arrays. • The movie's use of the term corresponds with the word's other definition, “something within or from which something else originates, develops, or takes form”
Declaring an array of arrays • When we declare an array in Java, we give the type, then use the [] to show it is a collection of that type.int[] arr; • When we declare an array of arrays, it works the same way. The type is an int[], so we have an int[][] (an array of int[]): int[][] matrix; • As with arrays, array of arrays can hold either primitive values (int[][]) or Objects (String[][])
Instantiating an array of arrays • When we instantiate an array in Java, we have to give the number of elements that will be stored in the array. int[] arr = new int[SIZE_OF_ARRAY]; • Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS. int[][] matrix = new int[NUM_ROWS][NUM_COLS];
0 1 2 3 4 0 1 2 3 4 A B C D E A B C D E 0 0 F G C I J F G H I J 1 1 K L M N O 2 2 K L M N O Example char[][] letters = new char[3][5]; letters[0][0] = 'A'; letters[2][1] = 'L'; letters[2][4] = 'O'; …
Instantiating an array of arrays • When we instantiate an array in Java, when can use the “shortcut method” to quickly fill up the array: int[] arr = { 3, 40, 2, 0 }; • Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS. int[][] matrix ={ { 8, 1, 3}, { 3, 2, 5}, { 1, 9, 8} };
0 1 2 3 4 0 1 2 3 4 A B C D E A B C D E 0 0 F G C I J F G H I J 1 1 K L M N O 2 2 K L M N O Example char[][] letters = { {'A', 'B', 'C', 'D', 'E'}, {'F', 'G', 'C', 'I', 'J'}, {'K', 'L', 'M', 'N', 'O'} }
Accessing elements in an array of arrays • We access an element in an array by giving the name of the array and brackets containing the index we want: int[] arr = new int[SIZE_OF_ARRAY];arr[0]=4; //first element of arr set to 4arr[arr.length-1]=4; //last element of arr set to 4 • Similarly, when we declare an array of arrays, we have to give the number of rows and number of columns the matrix will hold. Although how you visualize the data stored in the matrix is up to you, mathematics and programming conventions put the ROWS first, then the COLUMNS. int[][] m = new int[NUM_ROWS][NUM_COLS];//top-left element of arr set to 4m[0][0] = 4;//bottom-right element of arr set to 4m[m.length-1][m[m.length-1].length - 1] = 4;
0 0 1 1 2 2 3 3 4 4 A A B B C C D D E E 0 0 F F G G H H I X J J 1 1 K K L L M M N N O O 2 2 Example char[][] letters = { {'A', 'B', 'C', 'D', 'E'}, {'F', 'G', 'C', 'I', 'J'}, {'K', 'L', 'M', 'N', 'O'} }; letters[1][3]='X'; //row 1, column 3 assigned value 'X' Before Assignment After Assignment
Accessing Length • We access the length of an array with the .length property…no () arr.length • With matrices, accessing nameOfMatrix.length gives you the number of rows, and nameOfMatrix[0].length gives you the number of columns in the first row. matrix.length//number of rowsmatrix[0].length //number of columns in first row matrix[matrix.length-1].length //number of columns in last row
Ragged (Non-Rectangular) Array of Arrays • As mentioned before, in math matrices must be rectangular; all rows must have the same number of columns. • In Java, you can have ragged array of arrays, where each row has a different length. I do not suggest doing this often, as it can get confusing, but it can be done. • char[][] letters = { {'A', 'B', 'C'}, {'D', 'E', 'F', 'G'}, {'H', 'I' } }; • int[][] nums = new int[3][]; //3 rows, each of length not yet set nums[0] = new int[4]; //first row set to length 4 nums[1] = new int[1]; //second row set to length 1 nums[2] = new int[10]; //third row set to length 10
Looping through Matrices for(int r = 0; r<arr.length; r++) { for(int c = 0; c<arr[r].length; c++) { System.out.print(arr[r][c] + " "); } System.out.println(); //done with one row, go to next line }
Looping through Matrices w/For-Each Loop char[][] letters = //some already filled matrix of chars for(char[] arr : letters) { System.out.println(Arrays.toString(arr)); }