Menu
Topics Index
...
`


Array - Overview >
Siva Nookala - 20 Feb 2017
Arrays are used to store lots of similar data in one variable instead of multiple variables. That is, if we want to store the scores (or runs) of various cricket teams, we can either have india_score, pak_score, aus_score, srilanka_score or simply scores which contains the scores of all teams.

If we want to create variables to store the scores of the four teams and print the scores, we do the following.
int india_score = 200;
int pak_score = 190;
int aus_score = 210;
int srilanka_score = 195;

System.out.println("India = " + india_score);
System.out.println("Pak = " + pak_score);
System.out.println("Aus = " + aus_score);
System.out.println("Sri Lanka = " + srilanka_score);
But the problem with this approach, is if we have more teams like England or New Zealand, then we have to create more variables and hence increasing its complexity. To add to that when ever we have more teams, we have to modify the code and compile it.
To solve this problem, most of the programming languages provide Arrays. An array is a group of like-typed variables that are referred to by a common name.
india_score, pak_score, aus_score, srilanka_score are all like-typed variables since they have team scores and they are all integers. So they can be referred using a common name called scores.
The same program if modified to use arrays will look like:
Print team scores
class PrintTeamScores
{
    public static void main(String arg[])
    {
        int[] scores = new int[4]; // LINE A - Creating the scores array.
        
        scores[0] = 200;    // assigning score for team 0 or India
        scores[1] = 190;    // assigning score for team 1 or Pakistan
        scores[2] = 210;    // assigning score for team 2 or Australia
        scores[3] = 195;    // assigning score for team 3 or Sri Lanka
        
        System.out.println("India = " + scores[0]);
        System.out.println("Pak = " + scores[1]);
        System.out.println("Aus = " + scores[2]);
        System.out.println("Sri Lanka = " + scores[3]);    
    }
}
OUTPUT

India = 200
Pak = 190
Aus = 210
Sri Lanka = 195

DESCRIPTION

This program creates an array of scores with size 4. The values (or elements) of the array are initialized using indices and later printed using indices.

THINGS TO TRY
  • Add 3 new teams England, Bangladesh and New Zealand. To do this increase the size of the array from 4 to 7 i.e. change LINE A to int[] scores = new int[7]; Also initialize the elements scores[4], scores[5] and scores[6] and print them.
  • Since there are multiple values but only one variable scores, we need to use index (or unique number) to access the values (or elements). This index can be the team number. If we are storing the marks of various students, then the index can be student roll number. The above program uses numbers 0, 1, 2, 3 etc as indices for team numbers to the access the elements (values). Working with numbers is much more efficient and easier compared to working with variables like india_score, pak_score, aus_score etc.,. More details about the indices in Arraylist Access Using Index
  • Although in this example, the number of lines of code is not reduced, using arrays with loops will reduce the complexity and number of lines of code.
  • Arrays of any data type can be created, i.e. we can create arrays for int, double, char, boolean etc.,. But all the values in an array should be of same data type. We can not have one int and one double in the same array.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App