Array In Java with Example

An array is a Non-Primitive DataType in Java. An array is a collection of similar type variables.

For instance, an array of int is a collection of variables of the type int. The variables in the array are ordered and each has an index. The array is a data structure where we can store similar datatype elements. The array can store only fixed set of elements in java.

Array in Java

In the above example, we have an array of 8 indexes. In Java, Array always starts from index 0. Here, on the 0th index, we have value 12 and at last index 7th, we have value 23.

 

Declaration of Array

An array variable is declared just like you would declare a variable of the other types, except you need to add [] after the variable type.

Example —     int[] intArray ;

 

Instantiating an Array

In above example we declared an Java array variable , here we just created the reference to array itself. The declaration does not actually create an array. You create an array like this:

int[] intArray;

intArray = new int[8];

or

int[] intArray = new int[]{ 1,2,3,4,5,6,7,8};

In above example, we have created the int type Array intArray with space of 8 int type variables inside.

Accessing Array Elements

Array values are called array elements. In the above-created array intArray[] we have 8 int type elements. Each element in the array have its index, we can access the array element via their index value.

i.e we want to access the 5th index value in our array intArray[] –

int value = intArray[4] ;

Above code will copy the 5th index value of intArray into value variable.

( Note – Array start indexing from 0 so if you want to access the 5th value then actually you need to access the value present at index 4th).

Array Length

We can get the array length via its length field. i.e you want to calculate the length of intArray –

int arrayLenth = intArray.length ;

Above code will copy the array length into arrayLength variable.

Array Traversing

We can traverse the array using a loop. In the below-mentioned example, we are creating the Array and then accessing the array elements one by one.

 

Multi-Dimensional Array in Java

We can create a Multidimensional array in Java by appending one more square bracket ([]) per dimension we want to add.

int[][] intArray = new int[][];

In above example, we have declared two-dimensional arrays. You can create nth dimension array in Java. Look at the below mentioned example –

int[][] intArray = new int[3][4];

In above example, we are creating a two-dimensional array. Here in this example, first array index points out the array of the second index. It means in above example we have created 4 arrays and index of the first dimension are pointing the separate second dimension arrays.

 

Sample Programs on Array in Java

Insertion in Array

 

Find Minimum & maximum Value in Array

 

Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

0 Comment

Leave a Reply