Array
Array :- An Array is a collection of variables having the same name and data type. An Array
is a set of contiguous memory locations having the same name and data types. In Java Array
is the name of a class.
Types of Array:-
1) Single(One) Dimensional Array
2) Multidimensional Array.
Features of Array:-
1) A Non – primitive/ user defined / Reference / Composite Data type.
2) Stores large number of same type of data.
3) Stores data in continuous / contiguous memory location.
4) Has a name common for all the memory location.
5) Array is also known as Subscripted variable as Integer subscript or Index is used for
accessing each location
6) Each location can be accessed using the index number starting from 0 to n-1, where n is
the size of array.
Need of an Array:- They prove to be useful in situation where multiple data items
having the same data type are to be maintained by the program.
Declaration of array:-
i) Data type array_name[] = new data type[size];
ii) Data type []array_name = new data type[size];
iii) Data type[] array_name=new data type[size];
Eg:- double Ar[] = new double [7];
int []Ar=new int[5];
Ar 4bytes 4bytes 4bytes 4bytes 4bytes
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4]
Initialization of an array:-
1) int Num[]={3,5,10,12,8,17};
3 5 10 12 8 17
Num[0] Num[1] Num[2] Num[3] Num[4] Num[5]
System.out.println(Num[2]+ “\t”+ (Num[1]*10));
System.out.println( Num[3]-5 + “\t” + Num[4-2]);
2) char ch[ ] = {‘A’, ‘B’, ‘C’,’D’,’E’};
A B C D E
ch[0] ch[1] ch[2] ch[3]
ch[4]
System.out.println(ch[0] + ch[2]);
System.out.println(ch);
3) Initializing array with null:- Array being a reference data type can be
initialized with default value of reference data type that is “null”.
Example: int[] Marks= null;
double percent[]=null;
Accessing elements of an array:- Each data stored in an array is called its element
or member. The members or elements of the array can be accessed using the integer index
of the element which begins from 0 and ends in n-1, where n is the size of the array
(number of elements of the array).
Input in an array:- Data can be entered in the array using a loop which initializes from
0 till n-1.
// for an array(Ar) of int type
for( int i=0 ; i< n; i++)
{
System.out.println(“Enter element “+(i+1));
Ar[i]=sc.nextInt();
}
// for an array(Ar) of double type
for( int i=0 ; i< n; i++)
{
System.out.println(“Enter element “+(i+1));
Ar[i]=sc.nextDouble();
}
Displaying Array element:-Elements of the array can be displayed using the integer
index of the array like the way shown below
// for an array(Ar) of int type
System.out.println(“Elements of array are = “);
for( int i=0 ; i< n; i++)
{
System.out.println(Ar[i]);
}
Advantages and Disadvantage of an array:-
Advantages:-
1) Easy to specify: - The declaration and initialization can be done in one line of code.
2) Random access: - Data can be accessed randomly using its integer index number.
3) Easy code:- The code becomes more efficient and manageable as loops can be used to
carry out similar types of operation on all the elements of array.
4) Easy record handling:- Array of objects can be used to perform operations on
variables of user defined data types.
Disadvantages:-
1) Fixed size of an array:- once the size of array is set, it cannot be altered or changed
without the loss of data.
2) Insertion or deletion of elements make the program complex as it requires shifting of
elements.
3) Elements have to be of same type.
Base Address:- Base Address is the address of the first block of the array. It is this address
which is hold by the array name. Thus the name of the array is the address of the first
element of the array or the base address of the array.
length :- length is a static field property of array which returns the number of elements in
the array.
Syntax:- Arrayname.length.