Menu
Topics Index
...
`


Array - Overview >
Siva Nookala - 11 Apr 2016
Java supports multi dimensional arrays like 2 dimensions, 3 dimensions etc., A 2-dimensional array (2D array) is comprised of many one-dimensional (1D) arrays . They are also called "array of arrays". As discussed in Java Array, if we want to store cricket team scores, we used one dimensional (1D) array. If we want to store cricket team scores for 4 years, then we need two dimensional array.

For eg to store the scores of four teams (India, Pakistan, Australia and Srilanka) for the years 2008, 2009 and 2010, we could do the following.
int[] scores_2008 = new int[4];
int[] scores_2009 = new int[4];
int[] scores_2010 = new int[4];
or create a 2d array like
int[][] scores = new int[3][4];
Here first dimension is 3 since the number of years are 3, and the second dimension is 4 since number of teams are 4. The 2d array scores can be initialized as shown below.
scores[0][0] = 150; // Score in year 2008 (year 0) for India (team 0)
scores[0][1] = 170; // Score in year 2008 (year 0) for Pakistan (team 1)
scores[0][2] = 183; // Score in year 2008 (year 0) for Australia (team 2)
scores[0][3] = 216; // Score in year 2008 (year 0) for Srilanka (team 3)

scores[1][0] = 258; // Score in year 2009 (year 1) for India (team 0)
scores[1][1] = 221; // Score in year 2009 (year 1) for Pakistan (team 1)
scores[1][2] = 241; // Score in year 2009 (year 1) for Australia (team 2)
scores[1][3] = 230; // Score in year 2009 (year 1) for Srilanka (team 3)

scores[2][0] = 279; // Score in year 2010 (year 2) for India (team 0)
scores[2][1] = 260; // Score in year 2010 (year 2) for Pakistan (team 1)
scores[2][2] = 250; // Score in year 2010 (year 2) for Australia (team 2)
scores[2][3] = 274; // Score in year 2010 (year 2) for Srilanka (team 3)
Store team scores for 3 years
class Store3YearsTeamScores
{
    public static void main(String arg[])
    {
        // India - team 0
        // Pakistan - team 1
        // Australia - team 2
        // Srilanka - team 3
        // 2008 - year 0
        // 2009 - year 1
        // 2010 - year 2
        
        
        int[][] scores = new int[3][4];
        
        scores[0][0] = 150; // Score in year 2008 (year 0) for India (team 0)
        scores[0][1] = 170; // Score in year 2008 (year 0) for Pakistan (team 1)
        scores[0][2] = 183; // Score in year 2008 (year 0) for Australia (team 2)
        scores[0][3] = 216; // Score in year 2008 (year 0) for Srilanka (team 3)
        
        scores[1][0] = 258; // Score in year 2009 (year 1) for India (team 0)
        scores[1][1] = 221; // Score in year 2009 (year 1) for Pakistan (team 1)
        scores[1][2] = 241; // Score in year 2009 (year 1) for Australia (team 2)
        scores[1][3] = 230; // Score in year 2009 (year 1) for Srilanka (team 3)
        
        scores[2][0] = 279; // Score in year 2010 (year 2) for India (team 0)
        scores[2][1] = 260; // Score in year 2010 (year 2) for Pakistan (team 1)
        scores[2][2] = 250; // Score in year 2010 (year 2) for Australia (team 2)
        scores[2][3] = 274; // Score in year 2010 (year 2) for Srilanka (team 3)
        
        System.out.println("Srilanka scored " + scores[1][3] + " in the year 2009.");
        System.out.println("India scored " + scores[2][0] + " in the year 2010.");
        int total_australia_score = scores[0][2] + scores[1][2] + scores[2][2];
        System.out.println("Australia scored " + total_australia_score + " for years 2008, 2009 and 2010.");
        int all_teams_2009 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
        System.out.println("All teams scored " + all_teams_2009 + " in the year 2009.");
    
    }
}
OUTPUT

Srilanka scored 230 in the year 2009.
India scored 279 in the year 2010.
Australia scored 674 for years 2008, 2009 and 2010.
All teams scored 950 in the year 2009.

DESCRIPTION

Here we have declared a two dimensional array to store the team scores of various cricket teams in years 2008, 2009 and 2010. Every team is given a unique number (used as index) and every year is also given a unique number. The scores are initialized using indices and some of scores are printed, they are also accessed using indices.

THINGS TO TRY
  • Change Pakistan's score in year 2010 from 260 to 255 and add a print statement to print Pakistan's score.
  • Write code to print the total score achieved by all teams in the year 2010.
  • Write code to print the total score of achieved by India in all years.
  • A Two Dimensional Array (2D Array) can be compared with a matrix (or table), the first dimension represents the rows, the second dimension represents the columns. But the major difference between an array and matrix is, in array the number of columns in a row need not be same. So you can initialize the row dimension without initializing the columns, since the number of columns need not be constant for all the rows. e.g., we can five teams in year 2008, four teams in year 2009 and seven teams in year 2010. So a 2d array with variable column length can be declared as shown below.
int[][] scores = new int[3][]; // For 3 years 2008, 2009 and 2010
scores[0] = new int[5];     // Five teams in year 2008
scores[1] = new int[4];     // Four teams in year 2009
scores[2] = new int[7];     // Seven teams in year 2010

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.