A hybrid between 1D Arrays and ArrayLists, a 2D array is basically a matrix. A wealth of separate 1D Arrays stored in a table, with rows representing the arrays themselves and the columns representing the elements within them. Huge examples of this are games like Scrabble and Sudoku, where separate squares represent specific yet equally important values.
You could declare a 2D Array in TWO WAYS. You can create a empty 2D array with parameters, or create a already populated array with element already added according to your own choice.
To declare a 2D Array, start with a declaration of the file type, the signature of two square bracket combos and its file name; then, afterwards, have it equal to a file type + declaration of your parameters. Below is an example where I'm declaring a double 2D array called prices, and I'm tracking 4 price arrays, with 3 prices each. This is helpful in superstore management.
To declare a 2D Array that is pre-populated, declare the file type paired with the signature square bracket. Then set it equal to a wrapped curly bracket. Inside the curly bracket, add more enclosed curly brackets. The more enclosed curly brackets, the more arrays you create inside the 2D array. Inside the curly brackets you create, you can add elements as well, representing the inside of an array. Think of it this way: you create a huge 2d array (the big curly bracket surrounding), create arrays inside those 2d arrays (the inside curly brackets), and the elements of those same arrays (the values of the curly brackets)
If you used the first way to create an array, it will currently be empty. So, adding on, you can populate it by declaring individual separate 1D arrays. Remember, it has to abide by the parameters you set when you declared the 2D array originally.
In this example, I populated an empty 2D Array with separate 1D Arrays. I declared 4 separate 1D Arrays with 3 elements each because the parameters were 4 rows and 3 columns. You cannot go OVER 4 (or whatever you set it to) arrays or over 3 (or whatever you set it to) elements. If you go under the limit, the overflow will be considered a null, which is nothing, which, if untouched, can cause errors in the code.
All images have comments that direct you to the explanations.
REPLACE SPECIFIC ARRAY: You can set the 1D array variable equal to a brand new set of elements pretty easily, like in Screenshot 1. This can be done by declaring the parameter inside the square brackets.
REPLACE SPECIFIC ELEMENTS WITHIN IT: To replace elements within array, declare both the row and column parameters to narrow down to ONE specific element. For example, doubke[0][1] gets the first array, and the second element within it.
All images have comments that direct you to the explanations.
PRINT THE WHOLE 2D ARRAY: We can use a simple for loop to print the 2D Array. To grab the elements as well, just print the whole thing at type[0] and increment it by 1 each time to print each WHOLE 1D array each time.
ACCESSING SPECIFIC ELEMENTS/ARRAYS INDIVIDUALLY: To access each array and print it, you can use the parameters inside the square brackets again. Just get your 2D array, declare a row or column or both to narrow it down and just print it.
Row Major is a traversal technique used in Java where you look at it left to right EACH row at a time. It goes through all in the elements in the first row, then all the elements in a second row. Above is how it would look if you were to print it. Here we'd use a double loop as we loop going through ALL the elements first, and then loop moving to the next one.
i = row, j = col
Column Major is a traversal technique used in Java where you look at it lup to down EACH column at a time. It goes through all in the elements in the firstcolumn, then all the elements in a second column. Above is how it would look if you were to print it. Here we'd use a double loop as we loop going through ALL the elements first, and then loop moving to the next one.
i = row, j = col
Grabs the sum of all the values of the 2D array by using either row or column-major traversal. It'll use an accumulator to grab each value, so we can have a easy final answer at the end with no hassle.
Grabs the sum of all the values of the 2D array by using either row or column-major traversal. It'll use an accumulator to grab each value, so we can have a easy final answer at the end with no hassle. Then it'll use that final answer and divide it by the number of Runs counted to get the average.
Finds out the highest race by having a preset value of the first 2D array value as the lowest, then keeps updating it if the next value is higher UNTIL the end.
Finds out the lowest race by having a preset value of the first 2D array value as the lowest, then keeps updating it if the next value is lower UNTIL the end.