Page 326 - ComputerScience_Class_11
P. 326
In Program 1, we could see that more variables had to be created and the nextInt() method had to be called 10 times.
On the other hand, in Program 2, when we are applying an array, the size of the array is declared as required and only
the ‘for’ loop is executed 10 times for the calculation.
Thus, the advantage is that if the number of values to be taken is increased, then only the size of the array will be
increased along with the ‘condition’ portion of the loop, i.e., in this case, i<10 whereas in the first program more
variables had to be created for the same purpose.
So, using an array is definitely helpful when we have a large number of values (of similar data type) to be declared.
In the above programs, the numbers are stored in an array of int data type.
ar[ ] = Array to store integer numbers(Data type: int)
n[0] n[1] n[2] n[3] … ….. …. …. n[9]
45 65 98 74 … …. …. …. 82
Arrays are basically of two types. They are as follows:
• Single dimensional array
• Double dimensional array
11.3 SINGLE-DIMENSIONAL ARRAY
Two or more elements arranged row-wise containing separate subscript values indicating the position of the element
is known as a single-dimensional array or 1D array.
Let us understand how an array stores its elements in the memory. For example, a single-dimensional array car[ ] of
size 5 of String data type is as shown below:
0 1 2 3 4
car[ ] Maruti Ford Hundai Honda Tata
In the above example, the name of the array is car and 0,1,2, 3, 4 are the index positions of the elements. So, the 1st
element is referred to as car[0] containing the data Maruti, the 2nd element is referred to as car[1] containing the
value Ford and so on.
Note: The index of the array always begins with 0 (zero).
There can be two ways to declare an array which are static declaration and dynamic declaration. Let us understand
these declarations in detail.
11.3.1 Static Declaration
In this type of array declaration, the size of the array is fixed and the memory is allocated at compile time. In this
declaration, we need to specify the values or the elements of the array in the program itself. Let us see some examples
in which both the length of the array and the values are declared. These examples also demonstrate different data
types used with an array.
int array[ ]={1,2,3,4,5};
The above statement creates an integer array of size 5 and stores 5 integer elements in the array at compile time. Other
data types also have a similar type of declaration.
double array[ ]={1.2,3.4,1.5,76.4,55.5};
The above statement creates a double-type array of size 5 and stores 5 double-type values in the array at compile time.
char array[ ]={'a','B','2','5',':'};
324 Touchpad Computer Science (Ver. 3.0)-XI

