Anda di halaman 1dari 19

+

Lecture 10
Week 10

Arrays

Last Week
n

Arrays
n

Declaration
Instantiation

Accessing array items (use of index)

This Week
n

Passing array(s) to function(s)

Multi Dimensional Arrays

Strings

Output ?

void setup(){
int x = 10;
funct1(x);
println("In Setup "+x);
}
void funct1(int x){
x+=2;
println("In Function"+x);
}

Output ? Passing Arrays


void setup(){
int[] arr = {1,2,3,4,5,6,7};
funct1(arr);
for(int i =0; i < arr.length; i++)
println("In Setup "+arr[i]);
}
void funct1(int[] arr1){
for(int j = 0; j < arr1.length; j++){
arr1[j] *=2;
println("In Function "+arr1[j]);
}
}

Multi Dimensional Arrays


n

So far, we have seen the arrays with single dimension.

E.g.
n

int[] arr = {34, 67, 89, 0, 4, 5}

The above array has only one dimension


n

We can treat it as a single row of values.

Each value in a row can be accessed using the index of array.

What if
n

We have to store values in rows and columns.

E.g. marks of each assessment for each student in class.

Multi Dimensional Array

Student

Assessment 1

Assessment 2 Assessment 3

Student 1

89

97

65

Student 2

66

100

34

Student 3

78

93

65

Multi Dimensional Array

Multi Dimensional Array


n

Syntax
n

dataType[][] arrayName = new int [rows][column]

Example
n

int[][] studentMarks = new int[3][3]

The above example creates a two dimensional array of 3x3.

The array has 3 rows and 3 columns.

We can have a 2 dimensional array of any size.

A specific item within 2 dimensional array can be accessed


by providing the index numbers of both rows and columns.

10

Multi Dimensional Array


n

Example
n
n

An array x of 3 rows and 4 columns.


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

11

Traversing Two Dimensional Array


n

We can use iterative statements to traverse 2 dimensional


arrays.

We need to have 2 loops, one for rows and one for columns.

The loops need to be nested.

The outer loop can be used for rows and inner loop can be
used for columns.

12

Initializing Multidimensional Array


n int[][]

myArray = new int[4][4];


for(int i =0; i < 4; i++)
for(int j = 0; j < 4; j++)
myArray[i][j] = 2;

13

Explicit Initialization
n

int[][] myArray = {{1,3,5}, {3,5,7}, {5,7,11}, {7,11,13};

int[][] arr1 = {{1,


{3,
{5,
{7,

3, 5},
5, 7},
7, 11},
11, 13}};

14

Traversing Multidimensional
Arrays
int[][] myArray = new int[5][6];
for(int i =0; i < 5; i++)
for(int j = 0; j < 6; j++)
myArray[i][j] = i+j;
for(int i =0; i < 5 ; i++){
for(int j = 0; j < 6; j++){
print(myArray[i][j]+" ");
}
println();
}
Output ?

15

Exercise
n

Write code to declare a two dimensional array of 5X6.


Initialize the values of the array so that it look like:

1 2
2 4
3 6
4 8
5 10

3
6
9
12
15

4
8
12
16
20

5
10
15
20
25

6
12
18
24
30

16

Exercise
n

Assuming the following two dimensional array x.

1 2
2 4
3 6
4 8
5 10

Whats the value at

3
6
9
12
15

x[3][3]
x[4][2]

x[5][6]

4
8
12
16
20

5
10
15
20
25

6
12
18
24
30

17

Exercise
n

Assuming the following two dimensional array (arr) of size


5x5.

int[][] arr = new int[5][5];

Write a code to calculate and store the average of values in


each row of the array.

You need to create another array (one dimensional) to store


the average of each row.

18

Exercise
n

Write a function named equals that when passed two 2dimensional arrays of 3X3, returns true if both arrays are
equal and false otherwise.

Arrays are equal when all the elements of both arrays are
same.

10,20,30
77,39,45
89,44,23

is equals to

10,20,30
77,39,45
89,44,23

19

Anda mungkin juga menyukai