Description
How do you declare a variable for two-dimensional arrays? How do you create a two-dimensional array? How do you access elements in a two-dimensional array? This section will address these issues.
8.2.1 Declaring Variables of Two-Dimensional Arrays and Creating Two-Dimensional Arrays
The syntax for declaring a two-dimensional array is as follows:
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[][]; // This style is allowed, but not preferred
You can create a two-dimensional array of 5-by-5 int values and assign it to matrix using this syntax:
Figure 8.2
8.2.3 Ragged Arrays
Each row in a two-dimensional array is itself an array. Thus, the rows can have different lengths. An array of this kind is known as a ragged array. Here is an example of creating a ragged array:
8.2-3
As you can see, triangleArray[0].length is 5, triangleArray[1].length is 4, triangleArray[2].length is 3, triangleArray[3].length is 2, and triangleArray[4].length is 1.
If you don’t know the values in a ragged array in advance, but do know the sizes-say, the same as in the preceding figure-you can create a ragged array using the following syntax:
Two subscripts are used in a two-dimensional array: one for the row, and the other for the column. The two subscripts are conveniently called row index and column index. As in a one-dimensional array, the index for each subscript is of the int type and starts from 0, as shown in Figure 8.1a.
row index
column index
Figure 8.1
The index of each subscript of a two-dimensional array is an int value, starting from 0.
Figure 8.1
To assign the value 7 to a specific element at row index 2 and column index 1, as shown in Figure 8.1b, you can use the following syntax:
A Caution
It is a common mistake to use matrix[2,1] to access the element at row 2 and column 1. In Java, each subscript must be enclosed in a pair of square brackets.
You can also use an array initializer to declare, create, and initialize a two-dimensional array. For example, the following code in (a) creates an array with the specified initial values, as shown in Figure 8.1c. This is equivalent to the code in (b).
8.2-2
8.2.2 Obtaining the Lengths of Two-Dimensional Arrays
A two-dimensional array is actually an array in which each element is a one-dimensional array. The length of an array x is the number of elements in the array, which can be obtained using x.length. x[0], x[1], …, and x[x.length – 1] are arrays. Their lengths can be obtained using x[0].length, x[1].length, …, and x[x.length – 1].length.
For example, suppose that x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements, as shown in Figure 8.2. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4.
Figure 8.2
A two-dimensional array is a one-dimensional array in which each element is another one-dimensional array.
Ant: [[] z = langlahrazy – new int[5][]; triangleArray[3] = new int[2]; triangleArray[4] = new int[1]; You can now assign values to the array. For example,
Note: The syntax new int[5][] for creating an array requires the first index to be specified. The syntax new int[][] would be wrong.
Check Point:
8.2.1 Declare an array reference variable for a two-dimensional array of int values, create a 4by5 int matrix, and assign it to the variable.
8.2.2 Which of the following statements are valid?
Ant: [] = new int[2];
int[] x = new int[];
int[][] y = new int[3][];
int[][] z = {{1, 2}};
int[][] w = {{1, 2}, {2, 3}};
int[][] v = { {1, 2}, {2, 3}};
Mark Dowdle –
I am pleased for the assistance to get a answer to my questions. The service is prompt and provides good value to me.
Pablo Mendez –
great and amazing work, highly recommend. God bless you in all your days 🙂
Jeff Minyard –
The assignment was excellent and I have scored 100 percent. I am very proud and happy with you.
Amy Lowe –
Very impressive work and way ahead of time! Thanks!
Raheel Rahman –
I was very happy with everything in my assignment that I have reviewed. I have got great price and professional work
Tangela Arnwine –
Awesome work as usual!!