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.

  1. print all values in the array
  2. print out only the very first row?
  3. print out middle row (assuming there is only one 'middle' row...)
  4. print out last row
  5. print out only the elements in the last half of the row indexed by 2, if such a row exists
  6. print only those values on the "main diagonal"
  7. print only those values on the "minor diagonal"
  8. add one row to another ; print entire array before & after this operation
  9. multiply all values on one row by a scalar (ie, number); print entire array before & after this operation
  10. add one column to another ; print entire array before & after this operation
  11. create a square array of size, 30
      fill it with all zero's except along the main diagonal which should be all 1's.
      When a square matrix has these values it is called and "identity matrix"
    print entire array after this operation
  12. replace all values on the main diagonal with.... ; print entire array before & after this operation
  13. calculate the sum or average of all values in a row (in a column) (in the entire array)
  14. list all values on all rows with an even index ; one row per line of output
  15. load all values from a re-directed input file using Scanner

 

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?