



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Java Integer Array is a Java Array that contains integers as its elements. Elements of no other datatype are allowed in this array.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




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.
Following is the syntax to declare an Array of Integers in Java. or You can use any of these two notations.
To initialize an integer array, you can assign the array variable with new integer array of specific size as shown below. 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
int arrayName[]; int [] arrayName; arrayName = new int [size]; 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 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.
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. 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[] = new int [ 10 ]; } } public class IntArray { public static void main(String[] args) { int numbers[] = { 2 , 1 , 7 , 6 , 4 , 2 , 9 }; } } arrayName[index] 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); } }
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.
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. Related Tutorials How to fix ArrayIndexOutOfBoundsException in Java? How to fix ArrayStoreException in Java? JavaScript – Arrays How to Initialize Array in Java? Java – Iterate over Array Elements Java Concatenate Arrays Java Program – Find Smallest Number of an Array Java Program to Find Largest Number of an Array Java Program to Print Elements of Array Java Array – For Loop Java Array – While Loop How to find index of Element in Java Array? Java – Check if Array is Empty Java – Append to Array Array of Arrays in Java Array of Objects in Java Java Arrays asList() – Examples Java Arrays copyOf() – Examples Java Arrays sort() – Examples Java – How to Sort an Array only in Specific Index Range? 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]); } } } 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); } } }
Java System.arraycopy() – Syntax & Examples ⊩ Java Tutorial ⊩ Java Array ⊩ Java Array - Print ⊩ Java Array - Initialize ⊩ Java Array of Integers ⊩ Java Array of Strings ⊩ Java Array of Objects ⊩ Java Array of Arrays ⊩ Java Array - Iterate over Items ⊩ Java Array - For Loop ⊩ Java Array - While Loop ⊩ Java Array Append ⊩ Java Array - Check if Empty ⊩ Java Array Average ⊩ Java Array - Contains ⊩ Java Array - ForEach ⊩ Java Array - Find Index of Item ⊩ Java Array Sum ⊩ Java Concatenate Arrays ⊩ Java Array - Find Smallest Number ⊩ Java Array - Find Largest Number ⊩ Java Array - Reverse