Multi-Dimensional
Arrays

Arrays

Two-dimensional array

  • Examples:

    • Table of grades for each student and assignment

    • Table of data for each experiment and outcome

  • Mathematical abstraction: Matrix

  • Java abstraction: 2D array

Two-dimensional arrays in java

  • Array Access: use a[i][j] to access element in row i and column j.

 

  • Zero-based indexing: Row and column indices start at 0.

2D array or Array of Arrays

Example

Demo

Matrix Addition

Rows by columns or columns by rows

  • Consider int[][] m = new int[3][4];

  • Is that 3 rows by 4 columns or 3 columns by 4 rows?

  • The answer is that it can be either

    • As long as you are consistent with your column/row placement

Rows by columns or columns by rows

  • This makes it 3 columns by 4 rows

 

 

 

 

  • This makes it 3 rows by 4 columns

 

 

 

{J/R}agged Array

Jagged Array

Explicit Initialization

due soon

  • Strings and chars: 2.14,3.12-3.15,4.6 (Friday)
  • Methods: 6.1-6.12,6.17-6.19 (Monday)

Copy of 2D Arrays

By Narges Norouzi

Copy of 2D Arrays

  • 213