What is a 3 x 3 matrix?
That's 3 rows of 3 values each.
| 3 | -7 | 47 |
| -403 | -1 | 0 |
| 50 | 26 | 4 |
Let's use a 2D array of integers (doubles fine too...)
If the array is small, we can use the shortcut assignment again.
int [][] matrix = {
{ 3, -7, 99}
, { -403, -1, 0}
, { 50, 26, 4}
};
// oops, made a mistake. How to fix that?
matrix[0][2] = 47;
Because the array above has the same number of rows as it does columns it is called a "square" array
Ok, ok. What else might I ask/expect you to be able to do? With square arrays?
Things like the following or any variant.
Does an array have to be "square" ? No!
How many of the 'exercises' listed above make sense for an array like the below -- with a different number of rows than columns?
double vals[][]= {
{4.2, Math.PI, Math.E}
,{ 1, 2 , 3 }
,{ -1, 222 , 13 }
,{ 88, 2.2 , 4.023}
,{ 18, 0.2 , 44.8 }
,{ 3, .33333, 303.03}
};
Does an array have to have the same number of elements in every row? No!
Consider the following. It is called a "ragged" array because the different rows have different lenths.
int [][] elts = {
{ 3, -7, 99, 8, 42 }
, { -403 }
, { 50, 26, 4 }
, { 0, 0, 0, 1, -1, 0, 0, 100 }
};
How many of the 'exercises' listed above make sense for a "ragged" array? And what kind of adjustments to the corresponding code would we need to make in order to take the 'raggedness' into account?