1. Two Dimensional ArraysOften data comes naturally in two dimensional form. Web browser screens are two dimensional, as are spreadsheets, printed pages, and images. Show
For many of these situations you need a two-dimensional array. This is a collection of cells laid out in a 2D grid, like graph paper. Each cell can hold a value (as with a 1D array). However, now two indexes are needed to specify a cell. If you have ever used a spreadsheet then you have used a 2D grid for holding and manipulating data. One programming problem in the 2014 Advanced Placement test for computer science required use of a two dimensional array of object references. Chapter Topics:
Source: Bradley Kjell,
http://programmedlessons.org/Java9/chap67/ch67_01.html 2. Table of Student Grades
Imagine a class of 7 students that have a quiz every week for 5 weeks. The instructor records the grades in a table. A particular cell of the table is
designated by student number and week number. For example:
To specifying a cell use row and column indexes like this: gradeTable[ row ][ col ] As with one dimensional arrays, indices start at zero. For example:
3. 2D Arrays in Java
In Java, a table may be implemented as a 2D array. Each cell of the array is a variable that can hold a value and works like any variable. As with one dimensional arrays, every cell in a 2D array is of the same type. The type can be a primitive type or an object reference type. Important: Each cell of the array is specified with a row and column number, in that order. Say that gradeTable[ 0 ][ 1 ] // holds 42 The subscripted variables are used in assignment statements and arithmetic expressions just like any variable: // puts a 33 into row 0 column 1.
4. Headings are not part of the Array
The row and column numbers are not part of the array. They are usually shown in pictures of an array, but the array object does not explicitly contain the indexes. When you ask for
Java knows what cell you mean and goes there directly. More Important Stuff:
Details about these issues will follow.
5. Bounds Checking
When your program is running and it tries to access an element of an array, the Java virtual machine checks that the array element actually exists. This is called bounds checking. If your program tries to access an array element that does not exist, the Java virtual machine will generate an: ArrayIndexOutOfBoundsException Ordinarily, this will halt your program. Of course, as with 1D arrays, array indexes must be an integer type. It makes no sense to access As with a 1D array, an array index may be an integer literal, a variable of integer type, a method that evaluates to an integer, or an arithmetic expression involving all of these things:
We are not likely to need a statement as complicated as the last one.
6. 2D Array of int
The following program creates a 2D array of The declaration of
7. 2D Array Declaration
Two-dimensional arrays are objects. A variable such as says that int[][] myArray = new int[3][5] ; says that int[][] myArray = { {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0} }; does exactly the same thing as the previous declaration (and would not ordinarily be used.) The declaration int[][] myArray = { {8,1,2,2,9}, {1,9,4,0,3}, {0,3,0,0,7} }; creates an array of 3 rows and 5 columns and initializes the cells to specific values.
8. Different Numbers of Cells per Row
Different Numbers of Cells per Row
Each row of a 2D array may have a different number of cells. In the example, the array
If a program refers to a cell that does not exist, bounds checking will catch the error (as the program runs) and generate an exception (which usually will halt your program.) Notice that row 1 in the above has only two cells. (It is incorrect to think that row 1 has more cells but that only the first two are initialized.) Making an assignment to a cell that does not exist is an error.
9. Length of a 2D Array
Length of a 2D ArrayThe length of a 2D array is the number of rowsit has. You might guess that "length" could be defined as a number pair (rows, columns). But the number of columns may vary from row to row so this will not work. However, the number of rows does not change so it works as a length. Here is an example program:
10. Length of each Row
Length of each RowThe length of a 2D array is the number of rows it has. The row index runs from 0 to length-1. Each row of a 2D array can have a different number of cells, so each row has its own
11. Implementation of 2D Array
Implementation of 2D ArrayA two dimensional array is implemented as an array of one dimensional arrays. This is not as awful as you might think. It is an extension of what you already know about one dimensional arrays of objects. The declarationdeclares a variable To create an array of 3 rows do this: myArray = new int[3][] ; // 2. Now One way to create row 0 is this: myArray[0] = new int[3] ; // 3. This creates a 1D array object and puts its reference in cell 0 of A previously constructed 1D array can be assigned to a row: int[] x = {0, 2}; The rows do not need to have the same number of cells. The preceding statements construct the 2D array step-by-step. Usually you would not do this.
12. Individual Rows can be Replaced
Individual Rows can be ReplacedThe following answer will not work:An initializer list can only be used to initialize an array, not to assign values to it after it already exists. Perhaps your answer was: int[] x = {1, 9, 4} ; // declare and init x This will work, but does not quite do what the question asked. The suggested answer puts the values into the required cells of the existing 1D array object of row 0. The not-quite-right answer replaces the old row 0 with a new row. It constructs a new 1D array
object that holds the desired values in its cells, then assigns that object to row 0 of Both answers result in
13. Usual View of a 2D Array
Usual View of a 2D ArrayUsually you think of a 2D array as a table, not the complicated structure of the previous page. For example,int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; is usually thought of as like the table on the right. Only occasionally do you have to think of "arrays of references to 1D arrays." Now consider how you would print out every element contained
in array
14. Printing a 2D Array
Printing a 2D ArrayHere is a program that creates a 2D array, then prints it out. The nested loops are written to print out the correct number of cells for each row. The expression uneven[row].length evaluates to a different integer for each row of the array.
15. Null Rows
|
|
However, the code does assume that uneven
is non-null
.
Question 15:
(Thought Question: ) Are 3-dimensional arrays possible?
16. End of the Chapter
Answer:
Yes. But lets skip that for now. 2D arrays are usually as high as you need to go. 3D arrays get very big. A 3D array of 100
sheets of 2D arrays, where each 2D array has 100 rows and 100 columns would have 100*100*100 cells, 1,000,000 cells.
That is an awful lot of data. Usually some other type of data structure would be used.
End of the Chapter
You have reached the end this chapter. You may wish to review the following. Click on a subject that interests you to go to where it was discussed.
- array, two dimensional Two-dimensional array.
- array, row and column numbers Row and column numbering.
- bounds checking Bounds checking.
- array declaration, 2D Array declaration.
- array, 2D uneven rows Differing numbers of cells per row.
- array, length Length of a 2D array.
- array, row length Length of each row.
- array, implementation Implementation of a 2D array.
- array, 2D printing Printing a 2D array.