Page 279 - Computer science 868 Class 12
P. 279
But the program code becomes so lengthy and also difficult for him to deal with 25 different variables. The concept
of array is helpful in such situations. An array stores more than one variable of same data type. Now look at the code
given below which uses array to deal with same earlier situation of storing the names of 25 students.
import java.util.*;
class student
{
public static void main()
{
Scanner sc= new Scanner(System.in);
String name[]=new String[25];
int i;
System.out.println("Enter the name of the students one by one");
for(i=0;i<25;i++)
{
System.out.print("Enter name of student " +(i+1));
name[i]=sc.next();
}
System.out.println("The names of students are :");
for(i=0;i<25;i++)
{
System.out.println(name[i]);
}
}
}
Using array, the code has reduced in size and it has also become easier to deal with one array instead of 25 different
variables. Thus, an array helps to declare large number of variables of same type of data.
In the above example, the names of the students will be stored in a String array:
String name[] = new String[25];
Array to store 25 names (Data type: String)
name [0] name[1] name[2] name[3] … … … … name[24]
Ram Mukesh Shyam … … … … … Jay
Arrays can be defined as a set of variables which contains values of same data types having same variable name but
different subscripts required to separate the values. Thus, we can sat that an array is a composite data type.
Basically, there are two types of arrays.
• Single-dimensional Array
• Double-Dimensional Array
Let us understand them one by one.
9.2 SINGLE-DIMENSIONAL ARRAY/ONE-DIMENSIONAL ARRAY
Single-dimensional array or one-dimensional array is an array that deals with values of one data type. Here, single
subscript is used. Each index value refers to an individual array elements. The range of the index of the array starts from
0 and ends up to n-1 where n is the size of the array.
For example, a single-dimensional array marks[] of size 5 of int data type is shown below.
0 1 2 3 4
marks[] 56 89 98 34 100
Here, the marks of 5 students are stored in the marks[] array.
An array is created in the same way as an object is created. Two steps are involved while creating an array.
277
Arrays 277

