0% found this document useful (0 votes)
6 views

Lesson 3 Two Dimensional Array

This document provides an overview of two-dimensional arrays in computer programming, explaining their structure as a row-column matrix. It includes examples of declaration, initialization, and accessing elements of a two-dimensional array, as well as methods for processing the arrays. The document serves as a lesson for understanding the fundamental concepts of two-dimensional arrays.

Uploaded by

rhyaaolivia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lesson 3 Two Dimensional Array

This document provides an overview of two-dimensional arrays in computer programming, explaining their structure as a row-column matrix. It includes examples of declaration, initialization, and accessing elements of a two-dimensional array, as well as methods for processing the arrays. The document serves as a lesson for understanding the fundamental concepts of two-dimensional arrays.

Uploaded by

rhyaaolivia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIT 2

Arrays

CC 104 Computer Programming 2


LESSON

3 Two-Dimensional Array

CC 104 Computer Programming 2


Two-dimensional arrays are stored in a
row-column matrix, where the left index
Two- indicates the row and the right indicates
the column.
Dimension
Col 0 Col 1 Col 2 Col 3
al Array
Row 0 [0][0] [0][1] [0][2] [0][3]
Row 1 [1][0] [1][1] [1][2] [1][3]
Row 2 [2][0] [2][1] [2][2] [2][3]
Row 3 [3][0] [3][1] [3][2] [3][3]
LESSON 3
CC 104 Computer Programming 2
A typical declaration of two-dimensional
array is:
2D Array dataType[][] arrayName = new dataType[row][column];
Declaratio Example:
n
int[][] num = new int[2][3];

num[0][0] num[0][1] num[0][2]


num[1][0] num[1][1] num[1][2]

LESSON 3
CC 104 Computer Programming 2
Two–dimensional array can be initialized as:

2D Array int[][] num = {{ 10, 20,30},{40,50,60}};


Initializati 0 1 2

on 0 10 20 30

1 40 50 60

LESSON 3
CC 104 Computer Programming 2
To access the elements of a two –dimensional
array, you need a pair of indices: one for the
row position and one for the column position.
Accessing
Elements
Example 1 :

of a 2D System.out.print(num[0][1]);

Array //This statement will output the element from row 0


column 1 that is, the first row and the second column of
the array num

LESSON 3
CC 104 Computer Programming 2
Example 2 :

int[][] num = new[3][5]


Accessing num[0][2]=100;
Elements
//stores 100 into row 0 and column 2, that is,
of a 2D the first row and the third column of the array
num
Array

LESSON 3
CC 104 Computer Programming 2
A two-dimensional array can be processed in
three ways:

Processin 1. Process a particular row of the array, called


row processing.
g 2D
Array 2. Process a particular column of the array,
called column processing.

3. Process the entire array.


LESSON 3
CC 104 Computer Programming 2

You might also like