Page 363 - CA_Blue( J )_Class10
P. 363
St40_Name = "Aadarsh";
It easy to follow this process if the number of pens is less. What happened when the number of students is 1000 or
more? In that case, it is very time consuming and time taking process to maintain 1000 variables or more. But with the
help of arrays, it is very easy task to do so.
15.1.1 Array as a Composite Type
Composite types are those which contains a number of same or different types of data using a single declaration. As an
array is a collection of same type of data, so it can be referred as a composite date type or a non-primitive data type.
15.2 TYPES OF ARRAYS
In Java, there are two types of arrays:
• Single Dimensional Array
• Multi-Dimensional Array
Let us study about them in detail.
15.2.1 Single Dimensional Array
A single dimensional array is a type of array with single subscript. It contains the values either row-wise or
column-wise.
Declaring a Single Dimensional Array
We can declare a single dimensional array with the help of primitive data types. The syntax to declare a single
dimensional array is:
<data type> <array name>[] = new <data type>[size of array];
You can also use the first square brackets before the <array name> like:
<data type>[] <array name> = new <data type>[size of array];
Whare, <data type> is valid primitive data type like int, float, double, etc. <array name> is the valid Java identifier, [ ]
represents array declaration, new is the operator to initialize an array and [size of array] signifies the size of an array.
Size of an array is always be an integer number like 2, 4, 7, 12 20, etc. It represents the number of items can be stored
in an array. An item in an array is called an element. Each element of the array associate with an index started from 0.
Index is also known as subscript.
Let us create an array to store the names and roll numbers of 40 students in the following manner:
String names[] = new String[40]; //To store the names of students
int roll_no[] = new int[40]; //To store the roll numbers of the students
Java also allows you declare and initialize an array separately in the following manner:
String names[]; //Declaring an array
names = new names[40]; //Initializing an array
An array stores the values in contiguous memory locations. The memory representation of an array elements is shown
below:
Array Index names[0] names[1] names[2] names[3] … … … … names[39]
Values Amit Ajay Harry Raj … … … … Ramit
Memory Address 58421 58422 58423 58424 … … … … 58460
Array Index roll_no[0] roll_no[1] roll_no[2] roll_no[3] … … … roll_no[39]
Values 1 2 3 4 … … … 40
Memory Address 69406 69407 69408 69409 … … … 69445
361
Arrays 361

