Sie sind auf Seite 1von 25

MULTIDIMENSIONAL ARRAY

COURSE LEARNING OUTCOMES :


Upon completion of this course, the students should be able to:

1.Describe the features and basic concepts of Java


language.

2.Write, compile Java source code and interpret Java


byte code using Java Development Kit (JDK).

3.Implement the key of OOP concepts: classes and


objects, inheritance and polymorphism.

4.Incorporate exception handling in Java


Programming.

OBJECTIVES :

Write a program using

multidimensional array :

Define an array Declare and initialize an array Pass array to methods Return array to methods Write program using single array

2D Array
arrays with more than one dimension.

is a collection of a number of onedimensional arrays placed one below the other.


Two-dimensional arrays represent data in terms of rows and columns. It has two subscripts.

Representation -2D Array

Declare & Initialize


Assume that there are 5 students in a class and each of them study three different subjects, for example Mathematics, Physics and Chemistry. The marks of each student in all three subjects can be represented in a single row.

Representation -1D Array

Declaration -2D Array


Example

int marks_table[][] = new int[5][3];

Syntax

<Data type> <Variable name>[][] =


New <Data type>[Row][Column];

Representation -2D Array

Give the value of the following elements in the 2-D representation seen in Slide 10:

marks_table[0][0] marks_table[1][1]

marks_table[3][0]
marks_table[4][2]

Accessing Array Elements


You can use the row and column array index to access the array elements.

For example, if you need to print the value stored in first row and second column of the array marks_table, (i.e. chemistry mark of student 2) the code will be :
Example

System.out.println(marks_table[1][2]);

Enter Data to Array


A nested for loop can be used to enter data in a 2D array For example :
for (int x=0;x<2;x++) //data for row { for(int y=0;y<3;y++) //data for coloum { m = stdin.readLine(); marks_table[x][y] = Integer.parseInt(m); } }

Read Data from Array


for(int x=0;x<2;x++) //data for row
{

for(int y=0;y<3;y++) //data for


coloum

{ System.out.println("Marks : "+marks[x][y]); } }

Examine the following: double values[][] = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5, -1.2}, {7.3, 7.9, 4.8} } ;

What is the value of values[2][1]? a. 7.3 b. 7.9 c. 9.2 d. There is no such array element

You need to create an array as given :

12 7 -32

-9 14 -1

Choose the correct declaration from the declaration statements given: Declaration A Declaration B Declaration C Declaration D

double table [ ][ ] double table[ ][ ] double table[ ][ double table[ ][ = { 12, -9, 8, = { {12, -9, 8}, ] = { {12, -9, 8} ] = { {12, -9, 8}, 7, 14, {7, 14, 0}, {7, 14} {7, 14}, -32, -1, 0} ; -32, -1, 0} }; {-32, -1, 0} }; {-32, -1, 0} };

Which of the following statements constructs an array with 5 rows of 7 columns? long stuff[][] = new stuff[5][7]; long stuff[][] = new long[5][7]; long stuff[][] = long[5][7]; long stuff[][] = long[7][5];

Which of the following statements that constructs a two-dimensional array with 7 rows? int array[ ][ ] = new int[7][ ]; int array[ ][ ] = new int[][]; int array[ ][ ] = new int[ ][7]; int[] array[7] = new int[ ];

OOP in Java

Das könnte Ihnen auch gefallen