JAVA array introduction

JAVA array declaration initialization loop through java array tutorial

The JAVA array is used to store data of the same type.

If you use a dedicated JAVA class which represents an array like the ArrayList class, you can store data of different types in that object of type ArrayList.

In the JAVA array, the data is stored in adjacent boxes in memory.

In other languages like the C language, this allows to loop through an array using pointers.

JAVA array declaration syntax

ElementType  [] variableName;

Or we can use the C language like syntax:

ElementType   variableName [];

We declared a variable of type array of ElementType.

The name of the variable is variableName and the type of all the items stored in that array is ElementType.

Java Array examples:

Int [] tab;
Int tab [];
Char [] tab;
String [] tab;

JAVA array initialization syntax

The syntax of the creation of an empty array whose maximum size is nbMax and whose element type is ElementType is the following:

variableName = new ElementType [nbMax];

We can also declare and initialize an array in the same line:

ElementType  [] variableName = new ElementType [nbMax];

The assignement of a value called val in the position index in the array variableName is done like this:

variableName [index] = val;

Where index is an integer and the value is an expression of type ElementType or of an assignement compatible type.

The first index in an array is zero.

It means that the indexes of an array go from zero to nbMax minus one.

To access the element at index i, we use the notation variableName [i].

Example:

int [] tab1;
tab1 = new int [3];
tab1[0] = 1;
tab1[1] = 1;
tab1[2] = 1;

JAVA array loop through

We will loop through an array using the for loop and the foreach loop.

If you don’t know how to use the for loop, please check our for loop tutorial.

If you don’t know how to use the foreach loop, please check our foreach loop tutorial.

JAVA array loop through using the for loop

public class TestForLoop {
public static void main(String[] args) {
int [] tab ;
tab = new int [3];
tab[0] = 1;
tab[1] = 21;
tab[2] = 88;
for (int i = 0; i< 3; i++){
System.out.println("the element of index " + i + " is "+ tab[i]);
}
}
}
The output of this java program is the following:

the element of index 0 is 1

the element of index 1 is 21

the element of index 2 is 88

JAVA array loop through using the foreach loop

public class TestForeach {
 
public static void main(String[] args) {
 int [] tab ;
 tab = new int [3];
 tab[0] = 1;
 tab[1] = 21;
 tab[2] = 88;        
 for (int elem : tab){
 System.out.println("element: "+ elem);
 }
}
}

The output of this java program is the following:

element: 1

element: 21

element: 88

We remark that we haven’t access to the counter in the body of the java foreach loop.

We can modify the previous java program in order to have the same result as the java for loop program.
public class TestForeach {
public static void main(String[] args) {
int [] tab ;
tab = new int [3];
tab[0] = 1;
tab[1] = 21;
tab[2] = 88;
int i = 0;
for (int elem : tab){
System.out.println("the element of index " + i + " is "+ elem);
i++;
}
}
}

This core JAVA tutorial which deals with java arrays arrives at its end.

To check the other core JAVA tutorials, please check our core java tutorial.

Please, don’t forget to follow us on our How To Program Facebook page to get our fresh java tutorials.

Post a Comment

Previous Post Next Post