1. How to create a multidimensional array or 2D array.
2. How to iterate multidimensional arrays.
A Multidimensional Array, or a 2D array (even thought of as a matrix like in mathematics), is really just an array of arrays. For instance, we could have 3 different arrays, all with their own values, under a single variable name.
Multidimensional arrays have rows and columns. The rows refer to each array and the columns refer to the index of each row.
In order to create a multidimensional array, we have to use multiple brackets. The first set of brackets (row) refers to how many arrays we will have, the second set of brackets (column) refers to the length of each array.
To create multidimensional arrays we:
1. Data Type & two sets of brackets [] [].
2. variable name
3. assignment operator
4. new keyword
5. data type & two sets of brackets [] [].
6. The first bracket should include a number for how many arrays, the second should include a number for the length of each array.
7. semi-colon.
In the example, we created a multidimensional int array that had 3 arrays of length 4. The picture to the right should help understand how we visualize each element.
The array literal method still works for multidimensional arrays, but also creates a unique opportunity. In the previous method, every array had the same length, but with array literals we can create each array to be of a different size.
To create multidimensional array literal we:
1. Data Type & two sets of brackets [] [].
2. variable name
3. assignment operator
4. A set of brackes {}
5. Inside the set of braces, we include another set of braces for each array{}. All separated by a comma.
6. semi-colon.
The reason why it was crashing your raspberry pi's is that my videos are like 330MB. Which is WAY TOO BIG FOR CODING VIDS. I'll be making adjusts for next lesson so that they are in the 16MB range and way easier to stream from youtube as an embed.
Iterating through multidimensional arrays creates a unique challenge. A single array, we could access its length easily and loop through it till we reach that number. However, now we have multiple arrays and each could have a different length. This is where we will need nested for loops! An outer for loop to loop through how many arrays we have, and an inner for loop through the length of each array.
To get the number of arrays, we can still use array_name.length. But if each array could possibly have a different size, how do we get each arrays length returned at different times? To do this, we use array_name[array_number].length;
. This will return the length of the specific array number you pass. So if you want to know the length of the 0th array, you put 0. The first, you put 1 and so forth.
The reason why it was crashing your raspberry pi's is that my videos are like 330MB. Which is WAY TOO BIG FOR CODING VIDS. I'll be making adjusts for next lesson so that they are in the 16MB range and way easier to stream from youtube as an embed.
Think about your computer screen. Everything you are seeing is really just tiny squares illuminating with a single color. They are so small that it appears to be seamless. Each pixel has an x and a y position, or a row/column position. We often represent this with a multidimensional array. The data for each pixel would be the information about what color that pixel should be.
In the picture to the right, it is easy to see that there is an individual pixel. This is where computers become so handy. When you play a game at 60 FPS, that means the screen refreshes 60 times every second and changes the colors of all the pixels. It is a good thing that we can write programs that use multidimensional arrays to manage the pixels and changing of them!
Create a program that has 3 to 5 ASCII ARTs stored in a String multidimensional array of size 10x10.
Ask the user which they would like to print and then using a nested for loop, print it!
Ask the user how many rows and columns they want their matrix to be, then give them an opportunity to fill that double array with their input.
Then ask the user what they want to multiply the matrix by. (For the sake of this program, we will only multiple each element by one number and not by another matrix).
Then print it out!