Last Updated: 19 November, 2023
An array is an object used to store collections of elements of the same type. The length of an array is established when the array is created. Once an array is created, its size cannot be changed. An array allocates contiguous memory locations for each element in Java.
An array in Java is index-based; each element is accessed by its unique numerical index value, which starts at 0, which means the first element of the array will be stored at the 0th index position, the second element will be stored at the 1st index position, and so on. Using the index number, we can directly access any random value from the array.
An array with one dimension is called a one-dimensional array or single-dimensional array in Java. A Single Dimensional Array stores the same data-types values in a sequential fashion. Its length is fixed once created and cannot be changed.
A single-dimensional Array allocates contiguous memory locations for each element in Java.
Creating a Single-Dimensional Array
To create a single-dimensional array in Java, we can use the following syntax:
data_type[] array_name = new data_type[size];
For example, to create an array of integers with 5 elements, we would write:
int[] myArray = new int[5];
This creates an array named myArray of type int with a size of 5. The elements of the array are initialized to default values (0 for integers).
Accessing Array Elements
The index number of each element serves as its unique identification in Java. We can access individual elements of an array using their indices. The index of an array element starts at 0. For example, to access the first element of the myArray array, we would write:
int number = myArray[0];
Example 1: Java Array creation and Traversing
Output
Index: 0, Element: 20
Index: 1, Element: 40
Index: 2, Element: 60
Index: 3, Element: 80
Index: 4, Element: 100
Example 2: Array Declaration, Instantiation, and Initialization in a Single Statement
In this example, we will see how to declare, instantiate, and initialize an array in a single statement.
Output
Index: 0, Element: 5
Index: 1, Element: 10
Index: 2, Element: 15
Index: 3, Element: 20
Index: 4, Element: 25
Index: 5, Element: 30
Example 3: ArrayIndexOutOfBoundsException with Array
In an array, if we try to access an element beyond the range, invalid index numbers will throw an ArrayIndexOutOfBoundsException at run time.
The ArrayIndexOutOfBoundsException is a child class of IndexOutOfBoundsException, and it implements the Serializable interface.
Output
Index: 0, Element: 5
Index: 1, Element: 10
Index: 2, Element: 15
Index: 3, Element: 20
Index: 4, Element: 25
Index: 5, Element: 30
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
at com.javabytechie.singleD.array.ArrayIndexOutOfBoundsExceptionExample1.main(ArrayIndexOutOfBoundsExceptionExample1.java:11)
Example 4: Using for-each Loop with Array
For traversing an array in Java, we can use any loop, such as 'for' loop, 'while' loop, 'do-while' loop. In this example, we are using a 'for-each' loop to traverse an array.
Output
5
10
15
20
25
30
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com