Professional Documents
Culture Documents
Two-Dimensional Arrays: By: Jhaiona Hart Fuentes
Two-Dimensional Arrays: By: Jhaiona Hart Fuentes
DIMENSIONAL
ARRAYS
By: Jhaiona Hart Fuentes
“
Think of your crushes, You could have a one-
dimensional list of your crushes:
Example:
(kasabay sa jeep crush, crush sa school, crush na
taken, crush sa senior high, crush sa junior high, crush
na baller)
2
“
Or you could have a two-dimensional list of crushes,
each containing two crushes you like the most.
Example:
(kasabay sa jeep crush, crush sa school) and (crush na taken,
crush sa senior high) and (crush sa junior high, crush na
baller)
3
TWO-
DIMENSIONAL • In a two-dimensional, • For our purposes, it is better
or "2D," array, the to think of the two-dimensional
ARRAYS array as a matrix. A matrix can
elements can be
be thought of as a grid of
arranged in rows and
numbers, arranged in rows and
columns. columns, kind of like a bingo
board.
4
Example:
(second index)
0 1 2 3
0 4 6 2 5
row col
7 9 4 8
(first 1 rating [0] [2] = 2
index) rating [1] [3] = 8
6 9 3 7
5
Similarity
▹ Each element in the 2D array must by the
with 1 same type, either a primitive type or
Dimension object type.
array (1D)
▹ Array indices must be of type int and can
be a literal, variable, or expression.
Rating [3][j] = j;
▹ If an array element does not exists, the
Java runtime system will give you an
ArrayIndexOutOfBoundsException
6
To declare
the array
▹int [][], with two pairs of empty
variable
brackets.
and create
the array:
▹ int [][] A = new int [5][7];
* 5 rows, 7 columns
8
Example(int)
System.out.println (table
public class array { [0][0] + " " +
public static void main table [0][1] + " " +
(String [] args){
table [0][2]);
Output:
int table [][] = new int [3][3];
table [0][0] = 5;
System.out.println (table
table [0][1] = 14; [1][0] + " " + 5 14 17
table [0][2] = 17; table [1][1] + " " +
table [1][2]);
9 26 4
table [1][0] = 9; }
table [1][1] = 26; }
table [1][2] = 4;
9
E x a m p l e ( s t r i ng)
public class array {
public static void main (String [] args){
{
String fruits [][] =
Output:
{“Red", “Apple"}, Red Apple
{“Green", “Avocado"},
{“Purple", “Grapes"} Green Avocado
}; Purple Grapes
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits [i][0] + "\t" + fruits [i][1]);
}
}
} 10
Activity
“
Gawa kayo ng java program na
mag-o’output ng members of
your family. Include family
name and first name only. :>
11
Thank you for listening!