ARRAY IN JAVA
An array is a collection of similar data type element in contiguous memory location
referenced by a common name.
• Each element in an array is distinguished by the index or subscript. Index value starts
from 0.
2.3 4.7 112.55 66.10 88.0 91.78 1290 A[6]
ds[0] ds[1] ds[2] ds[3] ds[4] ds[5] 777 A[5]
double[] ds 124 A[4]
223 A[3]
4087 A[2]
11 A[1]
20 A[0]
int[] A
Two types of Syntax (rules to write) with Example
Type1 Type2
• datatype[] arrayname; datatype[] arrayname=new datatype[size];
• arrayname=new datatype[size];
• int[] regno; int[] regno=new int[5]; //array of 5
• regno=new int[5]; //array of 5 integer integer
Whenever an array object is created its elements are automatically initialized with 0 for integer
type (byte, short, int, long), 0.0 for floating point value (float, double), false for
Boolean,’\u0000’ for char and null for any reference type.
Types of Array
Arrays are classified into two types, they are
1) Single Dimensional(One Dimensional) []
2) Multi Dimensional [] []
Single Dimensional
Here Array has only one dimension; Below program shows input and output to an array (ah).
Explicit Array initialization (Two types of initialization possible)
Type1 Type2
int[] regno; int[] regno=new int[3];
regno=new int[3]; regno[0]=100;
int [] regno= {100, 89, 555}; regno[0]=100; regno[1]=89;
regno[1]=89; regno[2]=555;
regno[2]=555;
Demonstration of a character array
Finding Maximum element in an array
Finding Minimum element in an array
Finding Sum of the elements of an array
Sort the elements of an array in ascending or descending order
Arrangement of the elements in ascending or descending order is a common phenomenon in
any data structure. There are various types of sorting possible bubble, insertion, selection,
merge, quick, heap sort etc.
Multi Dimensional Array
In java multi dimensional array are actually arrays of array which is achieved by adding more
square bracket. A two-dimensional array can be like a table or matrix of rows and columns.
ar[0][0] ar[0][1]
2 1
ar[1][0] ar[1][1]
4 56
ar[2][0] ar[2][2]
7 81
ARRAY OF OBJECTS
An array can store objects of any class also. We can create array of objects in same way as you do it for primitive types.
PTO
An array of objects is created just like an array of primitive type data items in the following way.
Sample [] S = new Sample [7];
The above statement creates the array which can hold references to seven Sample objects. It doesn't create the Sample
objects themselves. They have to be created separately using the constructor of the Sample class. The S contains seven
memory spaces in which the address of seven Sample objects may be stored. If we try to access the Sample objects even
before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException
during runtime which indicates that S[0] isn't yet pointing to a Sample object.
S[0].marks = 100; Invalid
The Sample objects have to be instantiated using the constructor of the Sample class and their references should be assigned
to the array elements in the following way.
S[0] = new Sample ();
In this way, we create the other Sample objects also. If each of the Sample objects has to be created using a different
constructor, we use a statement similar to the above several times. We may use a for/while/do while loop since all Sample
objects are created with the same default constructor.
for (int i=0; i<[Link]; i++)
{
S[i]=new Sample(); 1
} 0
The above for loop creates seven Sample objects and assigns their reference to the array elements. Now, a statement like the
following would be valid.
S[0].marks=100;