While programming in Java, there can be a requirement to accumulate bulk data in an array. For instance, arranging and sorting the data to make it readable and accessible in an effective manner. In such instances, initializing and utilizing an array is assistive in managing the contained resources efficiently.
This blog will illustrate the approaches to initialize an array using Java.
How to Initialize an Array in Java?
An array can be initialized in multiple ways. These can be initializing it with the unassigned values, initializing it after declaring it, or with both the integer and string values at the same time.
Syntax
In the above syntax:
-
- “datatype” corresponds to the type of array data which can be an integer, string, etc.
- “[ ]” square brackets refer to the array size.
The discussed possibilities to initialize an array will now be illustrated one by one!
Example 1: Initialize an Array Without Assigning Values in Java
In this example, an array can be initialized without assigning values:
for (int i = 0; i < 3; i++){
System.out.println("The array with unassigned values is: "+ sampleArray[i]);
}
Apply the following steps in accordance with the above code:
-
- Firstly, initialize an array named “sampleArray” and define its size. i.e., “3”.
- After that, apply the “for” loop to iterate along the array and print it on the console.
Output
Since no elements are contained in an array, the iteration returns the value “0” at each of the array indexes.
Example 2: Initialize an Array After Declaration in Java
In this particular example, an array will be declared and initialized with integer values and the accumulated array values can be displayed on the console, respectively:
entries = new int[]{1,2,3};
for (int i = 0; i < 3; i++){
System.out.println("The array after initialization becomes: "+entries[i]);
}
In the above lines of code:
-
- First of all, declare an array named “entries”.
- In the next step, allocate the stated integer values to it.
- Lastly, apply the “for” loop to iterate along the array entries and display them.
Output
In the above output, it can be seen that the assigned values in the array have been displayed after iteration.
Example 3: Initialize and Allocate Values in an Array Simultaneously in Java
In this particular example, the initialization of an array and allocation of the values in it can be carried out simultaneously:
for (int i = 0; i < 3; i++){
System.out.println("The initialized array becomes: "+entries[i]);
}
Implement the following steps as given in the above code snippet:
-
- Initialize the array named “entries” and assign the stated values at the same time.
- In the next step, likewise, apply the “for” loop to iterate through the array values and display them.
Output
The above output indicates that the array declaration and initialization have been done appropriately.
Example 4: Initialize an Array With Both the Integer and String Values in Java
In this demonstration, an array will be initialized with both the integer and string values:
for (int i = 0; i < 5; i++) {
System.out.println("The initialized array becomes: "+entries[i]);
}
In the above lines of code:
-
- First, initialize an array named “entries” with both the integer and string values.
- Note that “Object” signifies that both the integer and string values can be accumulated in an array.
- Finally, likewise, apply the “for” loop to iterate along the array values and display them.
Output
That was all about initializing arrays in Java.
Conclusion
An array in Java can be initialized without assigning values, after the declaration, or with both the integer and string values. It is done with the help of “square brackets [ ]” and then allocating the values to it(array). These values can be integers, strings, or both. This blog discussed the approaches to initialize an array in Java.