Topic 2 Part 4.2

download Topic 2 Part 4.2

of 25

Transcript of Topic 2 Part 4.2

  • 7/30/2019 Topic 2 Part 4.2

    1/25

    MULTIDIMENSIONAL ARRAY

  • 7/30/2019 Topic 2 Part 4.2

    2/25

    COURSE LEARNING OUTCOMES :

    Upon completion of this course, the studentsshould be able to:

    1.Describe the features and basic concepts of Javalanguage.

    2.Write, compile Java source code and interpret Javabyte code using Java Development Kit (JDK).

    3.Implement the key of OOP concepts: classes andobjects, inheritance and polymorphism.

    4.Incorporate exception handling in JavaProgramming.

  • 7/30/2019 Topic 2 Part 4.2

    3/25

    OBJECTIVES :

    Write a program usingmultidimensional array :

    Define an arrayDeclare and initialize an array

    Pass array to methodsReturn array to methodsWrite program using single array

  • 7/30/2019 Topic 2 Part 4.2

    4/25

  • 7/30/2019 Topic 2 Part 4.2

    5/25

    arrays with more than one dimension.

    is a collection of a number of one-

    dimensional arrays placed one below

    the other.

    Two-dimensional arrays represent

    data in terms ofrows and columns. It

    has two subscripts.

    2D Array

  • 7/30/2019 Topic 2 Part 4.2

    6/25

    Representation -2D Array

  • 7/30/2019 Topic 2 Part 4.2

    7/25

    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.

  • 7/30/2019 Topic 2 Part 4.2

    8/25

    Representation -1D Array

  • 7/30/2019 Topic 2 Part 4.2

    9/25

    Example

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

    Syntax

    [][] =

    New [Row][Column];

    Declaration -2D Array

  • 7/30/2019 Topic 2 Part 4.2

    10/25

    Representation -2D Array

  • 7/30/2019 Topic 2 Part 4.2

    11/25

  • 7/30/2019 Topic 2 Part 4.2

    12/25

    Give the value of the followingelements in the 2-D representationseen in Slide 10:

    marks_table[0][0]

    marks_table[1][1]

    marks_table[3][0]

    marks_table[4][2]

  • 7/30/2019 Topic 2 Part 4.2

    13/25

    Accessing Array Elements

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

    For example, if you need to print the valuestored in first row and second column ofthe array marks_table, (i.e. chemistry markof student 2) the code will be :

    Example

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

  • 7/30/2019 Topic 2 Part 4.2

    14/25

    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

  • 7/30/2019 Topic 2 Part 4.2

    15/25

    Read Data from Array

    for(int x=0;x

  • 7/30/2019 Topic 2 Part 4.2

    16/25

  • 7/30/2019 Topic 2 Part 4.2

    17/25

    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}

    } ;

  • 7/30/2019 Topic 2 Part 4.2

    18/25

    What is the value of values[2][1]?

    a. 7.3

    b. 7.9

    c. 9.2d. There is no such array element

  • 7/30/2019 Topic 2 Part 4.2

    19/25

  • 7/30/2019 Topic 2 Part 4.2

    20/25

    You need to create an array as given :

    12 -9 8

    7 14

    -32 -1 0

  • 7/30/2019 Topic 2 Part 4.2

    21/25

    Choose the correct declaration from the declarationstatements given:

    Declaration A Declaration B Declaration C Declaration D

    double table [ ][ ]

    = { 12, -9, 8,7, 14,

    -32, -1, 0} ;

    double table[ ][ ]

    = { {12, -9, 8},{7, 14, 0},

    -32, -1, 0} };

    double table[ ][

    ] = { {12, -9, 8}{7, 14}

    {-32, -1, 0} };

    double table[ ][

    ] = { {12, -9, 8},{7, 14},

    {-32, -1, 0} };

  • 7/30/2019 Topic 2 Part 4.2

    22/25

  • 7/30/2019 Topic 2 Part 4.2

    23/25

    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];

  • 7/30/2019 Topic 2 Part 4.2

    24/25

    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[ ];

  • 7/30/2019 Topic 2 Part 4.2

    25/25

    OOP in Java