Академический Документы
Профессиональный Документы
Культура Документы
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; []
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:
fnali R O W S = 3; i nt fnali C O LS = 5; i nt .. .
/ i ant at a 3- 5 2D - r f var abl / nst i es byar ay or i e tabl e
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:
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.
For example, the following code will populate the entire two-dimensional array with the letters A to O:
char ch = A ;
number of rows
column index
The code above performs sequential access on the array. 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. Traditionally, TWO-DIMENSIONAL ARRAYS are graphically illustrated like the one shown below: column index Graphical illustration of the variable
row index
012340A 4
A B C D E 1FG H I 2 F J K K LM N O
COLUMN
tab l e
ROW A
ARRAY ELEMENT
0 012340ABCDE1FGHIJ2KLMN
F O O
0 012340A B C D E 1FG H I 2K L F J
M N O variable table
In here, 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, i.e. A, B, C, D, E, F, G, H, I, J, K, L, M, N, O 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.e. ROW 0: A, B, C, D, E table[1] refers to the second row of elements, i.e. ROW 1: F, G, H, I, J table[2] refers to the third row of elements, i.e. ROW 2: K, L, M, N, O using TWO (2) INDICES o refers to one specific array element defined by a row index and a column index. o example: table[0][0]refers to the array element at ROW 0, COLUMN 0, i.e. A table[1][4]refers to the array element at ROW 1, COLUMN 4, i.e. J table[2][3]refers to the array element at ROW 2, COLUMN 3, i.e. N
The nested FOR-loop construct on top of this page shows how array elements in a two-dimensional array are sequentially accessed. There are actually two ways to sequentially access two-dimensional arrays: ROW-WISE or COLUMN-WISE order. They differ in terms of the placement of the counter-controlled FOR-loop used to access the array. The following codes on the next page illustrates this.
ROW-WISE ACCESS
f ( or row = 0; row < tabl l e. ength; row + + ) { / top to b ottom / f ( = 0; col tabl or col < e[0]. ength; col + ) { / l to ri h t l + / eft g / ar / ray el ent accessed as tabl em s e[row ][col ] } } f ( or row = 0; row < tabl l e. ength; row + + ) { / top to b ottom / f ( = tabl or col e[0]. ength-1; col = 0; col - { / ri h t to l l > -) / g eft / ar / ray el ent accessed as tabl em s e[row ][col ] } } f ( or row = tabl l e. ength-1; row > = 0; row - ) { / b ottom to top / f ( = 0; col tabl or col < e[0]. ength; col + ) { / l to ri h t l + / eft g / ar / ray el ent accessed as tabl em s e[row ][col ] } } f row = tabl l or e. ength-1; row > = 0; row - ) { / b ottom to top / f ( = tabl or col e[0]. ength-1; col = 0; col - { / ri h t to l l > -) / g eft / ar / ray el ent accessed as tabl em s e[row ][col ]
} } } }
COLUMN-WISE ACCESS
f ( = 0; col tabl or col < e[0]. ength; col + ) { / l to ri h t l + / eft g f ( or row = 0; row < tabl l e. ength; row + + ) { / top to b ottom / / ar / ray el ent accessed as tabl em s e[row ][col ] } } f ( = 0; col tabl or col < e[0]. ength; col + ) { / l to ri h t l + / eft g f ( or row = tabl l e. ength-1; row > = 0; row - ) { / b ottom / / ar / ray el ent accessed as tabl em s e[row ][col ] } } f ( = tabl or col e[0]. ength- col = 0; col - { / ri h t to l l 1; > -) / g eft f ( or row = 0; row < tabl l e. ength; row + + ) { / top to b ottom / / ar / ray el ent accessed as tabl em s e[row ][col ] } } f ( = tabl or col e[0]. ength- col = 0; col - { / ri h t to l l 1; > -) / g eft f ( or row = tabl l e. ength-1; row > = 0; row - ) { / b ottom to top / / ar / ray el ent accessed as tabl em s e[row ][col ]
t top o
} } }
for (. . . { ) for (. . . { )
// print the
ENTIRE
System.out.printf("%2c", table[row][col]); }
// ... then move the cursor to the next line
row or column . . .
System.out.println(); }
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, left-to-right top-to-bottom, right-to-left bottom-to-top, left-to-right bottom-to-top, right-to-left
A B C DE F GHI J K L MN O
E DC B A J I HGF O N ML K
K L MN O F GHI J A B C DE
O N ML K J I HGF E DC B A
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.
}
Using INDEX i as ROW INDEX. Take note that index i should be replaced with an appropriately named index like r or row
/ / COLUMN-WISE access, i.e. table is accessed from theleft column to the right. up
for (int col=0; col<table[0].length; col++) { for (int i=0; i<table.length; i++) {
/ / array elements accessed table[i ] as: [co l ]
} }
- or -
// ROW WISE access, i.e. table is accessed from the top row down to the bottom. -
for (int i=0; i<table.length; i++) { for (int col=0; col<table[0].length; col++) {
} }
Using INDEX i as COLUMN INDEX. Take note that index i should be replaced with an appropriately named index like c or col
// ROW-WISE access
for (int row=0; row<table[0].length; row++) { for (int i=0; i<table.length; i++) {
/ / array elements accessed table[row] as: [i]
} }
// COLUMN WISE access -
- or -
for (int i=0; i<table[0].length; i++) { for (int row=0; row<table.length; row++) {
/ / array elements accessed table[row] as: [i]
} }
}
STRUCTURE-ITERATOR to access elements on a table in row-wise order from the top row down to the bottom. Note that array elements CANNOT be accessed in column-wise order using this form.
// read as: for each row in the table
for (in t [ ]
row : table) {
} }