Technical Note No. 114: Java 2D Arrays

[Pages:2]Technical Note No. 114 Java 2D Arrays

Published: September 8, 2013. Last reviewed on January 30, 2021 By Daryl Close, Professor of Computer Science and Philosophy, Heidelberg University, Tiffin, Ohio

Summary: This note reviews selected problems in using 2D arrays in Java.

Declaring and Initializing 2D Arrays

A Java 2D array is an array of arrays. For example, here is a 4x4 array of integers: int[][] puzzle = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16} };

However, for students learning to write algorithms to traverse a small 2D array, it is convenient to format your code in two dimensions, viz.,

int[][] puzzle = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };

The second declaration visually dramatizes the conventional metaphor that each internal array within the outer array is a row, whereas each element of a given row is a column. Remember that Java arrays are zero-indexed. So, for example, the index of the value 12 is puzzle[2][3].

Separating Declaration and Initialization

If you want to initialize the array in a constructor, for example, you must use the new operator as shown below. Note that new syntax is permitted, but not required, in the declare-and-initialize example above, but it is mandatory when initializing only.

int[][] puzzle; . . . puzzle = new int[][] { {1, 2, 3, 4},

{5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };

Traversing a 2D Array

In general, traversing a 2D array to print all the cells, sequentially search for a specific cell, etc., is most easily done with nested for-loops. The following code traverses the puzzle array, although it doesn't do anything with each array cell. Note that the number of columns is computed with an indexed expression, viz., "puzzle[row].length," while the number of rows, i.e., the outer array's size, is computed with "puzzle.length."

for (int row = 0; row < puzzle.length; row++) for (int col = 0; col < puzzle[row].length; col++) //do something with the array cell here

1 of 2

Java 2D Arrays Dr. Close

2 of 2

Here is a method that traverses a game board stored as a 2D array, returning the array contents in a String:

public String displayBoard() {

String boardString = "";

for (int row = 0; row < gameBoard.length; row++) {

for (int col = 0; col < gameBoard[row].length; col++) boardString = boardString + gameBoard[row][col] + " ";

boardString = boardString + "\n"; }

return boardString;

}//End method String displayBoard()

The general strategy here works for both square and rectangular arrays as well as so-called "ragged" arrays in which rows are of different lengths, i.e., have varying numbers of columns.

Copyright ? 2013-2021 Daryl Close This work is licensed under the Creative Commons Attribution-NoncommercialNoDerivatives 4.0 International Public License. To view a copy of this license, visit or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download