Contents
Data in a table or a matrix can be represented using a two-dimensional array.
The preceding chapter introduced how to use one-dimensional arrays to store linear collections of elements. You can use a two-dimensional array to store a matrix or a table. For example, the following table that lists the distances between cities can be stored using a twodimensional array named distances .
Two-Dimensional Array Basics
How do you declare a variable for two-dimensional arrays? How do you create a twodimensional
array? How do you access elements in a two-dimensional array? This section addresses these issues.
Declaring Variables of Two-Dimensional Arrays and Creating Two-Dimensional Arrays
The syntax for declaring a two-dimensional array is:
elementType[][] arrayRefVar;
or
elementType arrayRefVar[][];
// Allowed, but not preferred
As an example, here is how you would declare a two-dimensional array variable matrix
of int values:
int[][] matrix;
or
int matrix[][];
You can also use an array initializer to declare, create, and initialize a two-dimensional
array.