Java Integer Array
Java Integer Array is a Java Array that contains integers as its elements. Elements of no other datatype are allowed in this array.
In this tutorial, we will learn how to declare a Java Int Array, how to initialize a Java Int Array, how to access elements of it, etc.
How to declare an Integer Array in Java?
Following is the syntax to declare an Array of Integers in Java.
int arrayName[];
or
int[] arrayName;
You can use any of these two notations.
How to initialize an Integer Array?
To initialize an integer array, you can assign the array variable with new integer array of specific size as shown below.
arrayName = new int[size];
You have to mention the size of array during initialization. This will create an int array in memory, with all elements initialized to their corresponding static default value.
The default value for an int is 0
.
Following is an example program to initialize an int array of size 10.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[];
numbers = new int[10];
}
}
We have declared and initialized the int array in two different statements. But you can combine the declaration and initialization, to form the definition of int array, as shown below.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = new int[10];
}
}
In the above example, we have created a string array named numbers, and initialized it to an int array of size 10 with default values of 0.
You can also assign int values directly to the integer array when declaring it.
In the following example, we have declared and initialized int array with elements.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
}
}
Now numbers is an integer array with size of 7, because there are seven elements in the array we assigned.
How to access Integer Array Elements?
Following is the syntax to access an element of an array using index.
arrayName[index]
The above statement can be used either to read an element at given index, or set an element at given index. The read or write operation depends on which side of assignment operator you are placing this.
For example, in the following program, we are reading the element of int array at index 2.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
int number = numbers[2];
System.out.println(number);
}
}
Output
7
And in the following example, we are updating the element of int array at index 2 to 85.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
numbers[2] = 85;
System.out.println(numbers[2]);
}
}
Output
85
How to iterate over array elements?
We can use any of these looping statements like For Loop or While Loop to iterate over elements of a Java Array.
In the following example, iterate over elements of Integer Array using Java While Loop.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
int index = 0;
while (index < numbers.length) {
System.out.println(numbers[index]);
index++;
}
}
}
Output
2
1
7
6
4
2
9
In the following example, we will iterate over elements of Integer Array using Java For Loop.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
for (int index = 0; index < numbers.length; index++) {
System.out.println(numbers[index]);
}
}
}
And in the following example, we will use Java for-each loop, to iterate over elements of integer array. For each loop can be used to execute a set of statements for each string in integer array.
Java Program
public class IntArray {
public static void main(String[] args) {
int numbers[] = {2, 1, 7, 6, 4, 2, 9};
for (int number: numbers) {
System.out.println(number);
}
}
}
Conclusion
In this Java Tutorial, we learned about Integer Array in specific: how to declare an int array, how to initialize it, how to access elements, etc.