Multi-Dimensional Arrays

A MULTI-DIMENSIONAL ARRAY is an array with two or more dimensions. Unlike one-dimensional arrays which organize data as a list of array elements, multi-dimensional arrays organize data in a more complex manner – for example, with two dimensions, data is organized into a table (of array elements) and with three dimensions, data is organized into a list of tables (of array elements). To access individual array elements, one-dimensional arrays require one index to access an element in the array while multi-dimensional arrays require multiple indices to access a element in the array. The number of indices required to access an element depends on the number of dimensions that was specified for the array.

Two-Dimensional Arrays
TWO-DIMENSIONAL ARRAYS are multi-dimensional array with two dimensions. They are used to represent tabular data or any two-dimensional objects. They organize data into a table that consists of rows of array elements. Given this organization, individual array elements in a two-dimensional arrays are accessed using two indices to represent the two dimensions in the array – the first index is the row index and the second is the column index. Two-dimensional arrays are declared by appending two pairs of square brace delimiters, e.g. [], after the base type. The code below defines the variable named table to reference TWO-DIMENSIONAL ARRAYS consisting of character elements (since the base type is char)

char[][] table;

// declaration for a 2D-array variable ‘table’

The following code instantiates a TWO-DIMENSIONAL ARRAY of character elements organized into a tabular format consisting of THREE (3) rows and FIVE (5) columns:

final int ROWS = 3; final int COLS = 5; ...
// instantiates a 3-by-5 2D-array for variable ‘table’

table = new char[ ROWS ][ COLS ];
row size NOTE:

column size

1. ROWS and COLS indicate the size of the array (as row size and column size respectively). Given the row size and column size, the number of array elements that can be stored in the twodimensional array is computed as the product of the row size and the column size. Thus, table can store fifteen (15) array elements; i.e. computed as 3 x 5. 2. Furthermore, given the size, the valid index to access individual array elements is any nonnegative integer number less than the size. Thus, row index is from 0 to ... < row size and column index is from 0 to ... < column size 3. Regardless of the variable name, the first size is always the row size and the second is the column size. Thus if the statement was written as:

table = new char[COLS][ROWS]
COLS and ROWS will be the row size and columns size respectively. Therefore, the twodimensional array will have FIVE rows and THREE columns instead. table can store fifteen (15) array elements; i.e. computed as 5 x 3.

Operations performed on individual array elements are naturally repetitive and there is usually a need to sequentially access all the elements in some orderly fashion – as opposed to randomly accessing these elements. To do this, two (2) counter-controlled FOR-loops, one nested inside the other, are used where the counter in each loop will also serve as an index in the two-dimensional array. One counter will be used as a row index and the other counter will be used as a column index.

D. i. } } row index number of rows for (int col = 0. N. Traditionally. F.e.e. H. M. the array can be referenced as follows: • using ZERO (0) INDICES o refers to the entire two-dimensional array o example: table refers to the entire array of array elements organized into rows and columns. G. ROW 1: F. COLUMN 0. A table[1][4] refers to the array element at ROW 1. E. i. E table[1] refers to the second row of elements. J. I. B. A. I. This code allows ALL array elements to be accessed and accessed in a very orderly fashion starting with the first row all the way to the last.For example. row < table. ROW 0: A. J table[2][3] refers to the array element at ROW 2.e. O using TWO (2) INDICES o refers to one specific array element defined by a row index and a column index. N • .e. col++) { number of columns column index The code above performs sequential access on the array. D. row++) { table[ row ][ col ] = ch. o example: table[0][0] refers to the array element at ROW 0. O 012340 ABCDE1FGHIJ2KL MNOvariable table • using ONE (1) INDEX o refers to one entire row of array elements defined by a row index o example: table[0] refers to the first row of elements. i. L. COLUMN 3. i. C. H. i. G. i. L. for (int row = 0.e.length. K.e. N.e. COLUMN 4. col < table[0]. M. ch ++. ROW 2: K. B.length. the following code will populate the entire two-dimensional array with the letters ‘A’ to ‘O’: char ch = ‘A’. TWO-DIMENSIONAL ARRAYS are graphically illustrated like the one shown below: column index Graphical illustration of the variable row index 012340 ABCDE1FGHIJ 2KLMNO  COLUMN table  ROW ARRAY ELEMENT  O 012340ABCDE1FGHIJ2KLMN TWO DIMENSIONAL ARRAY In here. J table[2] refers to the third row of elements. C. i.

length-1. col>=0. col>=0.length-1.length.length-1.length.length. row++) { // top to bottom // array elements accessed as table[row][col] } } for (col=0. ROW-WISE ACCESS for (row=0. The following codes on the next page illustrates this..length.length. col--) { // right to left // array elements accessed as table[row][col] } }  COLUMN-WISE ACCESS for (col=0. There are actually two ways to sequentially access two-dimensional arrays: ROW-WISE or COLUMN-WISE order.. row--) { // bottom to top for (col=table[0]. col--) { // right to left for (row=table.length. table[row][col]). col>=0.length-1. col>=0.out.length. row>=0. col--) { // right to left for (row=0. if the loop body in the nest FOR-loop is implemented as: for (.printf("%2c".length..The nested FOR-loop construct on top of this page shows how array elements in a two-dimensional array are sequentially accessed. row<table. row<table.) { // print the ENTIRE System. row>=0. col--) { // right to left // array elements accessed as table[row][col]  } } for (row=table.) { for (. row++) { // top to bottom for (col=table[0].length-1. col<table[0]. row<table. They differ in terms of the placement of the counter-controlled FOR-loop used to access the array. row--) { // bottom to top // array elements accessed as table[row][col] } } for (col=table[0]. row--) { // bottom to top // array elements accessed as table[row][col] } }    Example. col++) { // left to right for (row=table.length-1. row++) { // top to bottom for (col=0.. col<table[0]. col++) { // left to right // array elements accessed as table[row][col]  } } for (row=0. row--) { // bottom to top for (col=0. row>=0.length-1.length-1.. row<table. row++) { // top to bottom // array elements accessed as table[row][col]  } } for (col=table[0]. row>=0. col<table[0]. .. col<table[0]. } row or column . col++) { // left to right // array elements accessed as table[row][col]  } } for row=table. col++) { // left to right for (row=0.

i<table.. i. then move the cursor to the next line System.e.println(). i++) { // array elements accessed as: table[i][col] } } // ROW-WISE .e. right-to-left ABCDE FGHIJ KLMNO EDCBA JIHGF ONMLK KLMNO FGHIJ ABCDE ONMLK JIHGF EDCBA Then the following output is generated using COLUMN-WISE access: left-to-right. for (int col=0. i.out. col++) { for (int i=0.length.or access. top-to-bottom right-to-left. i++) { // array elements accessed as: list[i] } Using INDEX i as ROW INDEX. bottom-to-top right-to-left. table is accessed from the left column up to the right.// . for (int i=0. left-to-right bottom-to-top. . bottom-to-top A F K B G L C H M D I N E J O K F A L G B M H C N I D O J E E J O D I N C H M B G L A F K O J E N I D M H C L G B K F A Converting ONE-DIMENSIONAL ARRAY operation for TWO-DIMENSIONAL ARRAYS Using Counter-controlled Iteration COUNTER-CONTROLLED ITERATION to access elements on a one-dimensional array list from left-to-right. Take note that index ‘i’ should be replaced with an appropriately named index like ‘r’ or ‘row’ // COLUMN-WISE access.length. left-to-right top-to-bottom. right-to-left bottom-to-top.length. } And table is populated as: 0 0 1 2 1 2 3 4 A F K B G L C H M D I N E J O variable table Then the following output is generated using ROW-WISE access: top-to-bottom. col<table[0].. top-to-bottom left-to-right. i<list. table is accessed from the top row down to the bottom.

col++) { // array elements accessed as: table[i][col] } } .for (int i=0. col<table[0].length.length. i<table. i++) { for (int col=0.

Note that array elements CANNOT be accessed in column-wise order using this form.Using INDEX i as COLUMN INDEX. row++) { // array elements accessed as: table[row][i] } } Converting ONE-DIMENSIONAL ARRAY operation for TWO-DIMENSIONAL ARRAYS Using Structure-Iterator STRUCTURE-ITERATOR.length. FOR-EACH LOOP.or access for (int i=0. row++) { for (int i=0. i<table[0].length.length. // read as: “for each row in the table” for (int[] row: table) { // read as: “for each element in the row” for (int element: row) { // array elements accessed as: element } } . row<table. to access elements on a list from left-to-right // read as: “for each element in the list” for (int element: list) { // array elements accessed as: element } STRUCTURE-ITERATOR to access elements on a table in row-wise order from the top row down to the bottom. a. i++) { for (int row=0.length. row<table[0]. i++) { // array elements accessed as: table[row][i] } } // COLUMN-WISE . Take note that index ‘i’ should be replaced with an appropriately named index like ‘c’ or ‘col’ // ROW-WISE access for (int row=0.k. i<table.a.

Sign up to vote on this title
UsefulNot useful