# Matrices or 2-Dimensional Arrays or Arrays of Arrays.

## Presentation on theme: "Matrices or 2-Dimensional Arrays or Arrays of Arrays."— Presentation transcript:

Matrices or 2-Dimensional Arrays or Arrays of Arrays

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) ONMLK JIHGF EDCBA 0 1 2 01234 Model 1Model 2 EDCBA JICGF ONMLK 01234 0 1 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 WikipediaWikipedia

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 WikipediaWikipedia

Places you may have heard of matrices The use of the term “matrix” in the horrible 1999 movie The Matrix doesn't really 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];

Example char[][] letters = new char[3][5]; letters[0][0] = 'A'; letters[2][1] = 'L'; letters[2][4] = 'O'; … ONMLK JIHGF EDCBA 0 1 2 01234 EDCBA JICGF ONMLK 01234 0 1 2

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} };

Example char[][] letters = { {'A', 'B', 'C', 'D', 'E'}, {'F', 'G', 'C', 'I', 'J'}, {'K', 'L', 'M', 'N', 'O'} } ONMLK JIHGF EDCBA 0 1 2 01234 EDCBA JICGF ONMLK 01234 0 1 2

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 4 arr[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 4 m[0][0] = 4; //bottom-right element of arr set to 4 m[m.length-1][m[m.length-1].length - 1] = 4;

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' ONMLK JIHGF EDCBA 0 1 2 01234 ONMLK JXHGF EDCBA 0 1 2 01234 Before AssignmentAfter 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 rows matrix[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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035764/slides/slide_15.jpg", "name": "Looping through Matrices for(int r = 0; r

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)); }