Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

TWO-

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.

• Two-dimensional (2D) •A 2D array is a collection of


arrays are indexed by data cells, all of the same type,
which can be given a single
two subscripts, one for name. However, a 2D array is
the row and one for the organized as a matrix with a
column. number of rows and columns.

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

▹ The base type of a 2D array can be


anything, so you can have arrays of
type double[][], String[][], and so on.
7
Declaring
2- ▹ Declare a local variable rating that references a 2D array of
Dimensional int:
Arrays int [][] rating;

▹ Create a 2D array with 3 rows and 4 columns and assign


the reference to the new array to rating:
rating = new int [3][4];

▹ Shortcut to declare and create a 2D array:


int [][] rating = new int [3][4];

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!

Sana may natutunan kayo hekhekhek


HAHAHAHA.
12

You might also like