What’s an Array

  • Eclipse: Oxygen
  • Java: 1.8

The array is basically a collection, an arrangement of similar datatypes entities. Thus, for a simple example, it is used to assign a common variable name for accessing a large number of entities of the same datatype by the difference of the index number.

The array as seen above is a collection of entities which have similar data types, thus this tutorial deals with the initialization of an array. And various other datatypes. Also, how to access the array and certain elements located at various index is shown here.

Declaration of an array in Java:

nitialization of  an array:

The length of the array is determined by the number of values provided that are separated by commas. In this example, the length of intVariableArray is 10.

You can easily access and alter the elements of an array by using its numeric index. The default initial value of index of an element of an array is 0.

Output

6

4.0

Foreach loop

Here we deal with the for and foreach loop which is the advanced version of for loop.  The for loop can be used to iterate over the elements of the array. The for each loop is a shorter version of the same.

This is the ordinary approach of the “for” loop. In the following example, you can see the use of index of the array as a counter.

Output

Chaand

John

Pooja

Mia

Salim

In the following example, you can see the difference between the loops. For-Each Loop helps us to reduces the code significantly, and there is no use of the index or, rather the counter in the loop.

Output

Chaand

John

Pooja

Mia

Salim

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu