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
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.
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
The same program if modified to use arrays
will look like:
Print team scores
public static void main(String arg)
int scores = new int; // LINE A - Creating the scores array.
scores = 200; // assigning score for team 0 or India
scores = 190; // assigning score for team 1 or Pakistan
scores = 210; // assigning score for team 2 or Australia
scores = 195; // assigning score for team 3 or Sri Lanka
System.out.println("India = " + scores);
System.out.println("Pak = " + scores);
System.out.println("Aus = " + scores);
System.out.println("Sri Lanka = " + scores);
India = 200
Pak = 190
Aus = 210
Sri Lanka = 195
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
7 i.e. change
LINE A to
int scores = new int; Also initialize the elements
scores 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
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.