Java How To Create Empty Array
- HowTo
- Java Howtos
- Initialize an Empty Array in Java
Initialize an Empty Array in Java
Created: December-10, 2020
-
new
Keyword to Declare an Empty Array in Java - Declare of an Empty Array Using
new
Keyword With Predefined Size - Initialize an Array Without Using
new
Keyword
This tutorial article will introduce how to initialize an empty array in Java.
There are several ways to declare an array in Java, but we can only do this dynamically.
new
Keyword to Declare an Empty Array in Java
The new
keyword initiates an object dynamically (runtime allocation of memory), and returns the reference of that object's memory.
To declare an empty array in Java, we can use the new
keyword. After the declaration of an empty array, we can initialize it using different ways.
The syntax of declaring an empty array is as follows.
data-type[] array-name = new data-type[size]; //or data-type array-name[] = new data-type[size];
There are two major ways to declare an empty array in Java using the new
keyword that is as follows.
Declare of an Empty Array Using new
Keyword With Predefined Size
We can declare an empty array using the new
keyword with a predefined size. In this case, we will provide the size to the array before runtime, and then the array will be declared according to the size.
The example code of the declaration of an empty array by predefined size in Java and then initialize that array's values are as follows.
public class Declare_Empty_Array { public static void main(String args[]) { int Size = 5; int array[] =new int[Size]; for(int i=0;i<Size;i++) { array[i] = i+1; System.out.println("The value stored in array on index "+i+" is: "+array[i]); } } }
In this above code, we declare an empty array with a predefined size and then initialize that array's values using the for
loop. We can also use the while
loop for the same purpose.
The output of the code is as follows.
The value stored in the array on index 0 is: 1 The value stored in the array on index 1 is: 2 The value stored in the array on index 2 is: 3 The value stored in the array on index 3 is: 4 The value stored in the array on index 4 is: 5
Initialize an Array Without Using new
Keyword
There is another way to initialize an array and then update its values without using the new
keyword. In this method, we can initialize the array with predefined values and update them with our desired values.
import java.util.Scanner; public class Declare_Empty_Array { public static void main(String args[]) { int array[] = {5, 5, 5, 5, 5}; for(int i=0;i<array.length;i++) { array[i] = i+1; System.out.println("The value updated in array on index "+i+" is: "+array[i]); } } }
In the above code, we initialize an array that has 5 stored in it initially, and after that, we can update its values.
The output of the code is as follows.
The value updated in array on index 0 is: 1 The value updated in array on index 1 is: 2 The value updated in array on index 2 is: 3 The value updated in array on index 3 is: 4 The value updated in array on index 4 is: 5
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.
Related Article - Java Array
Java How To Create Empty Array
Source: https://www.delftstack.com/howto/java/initialize-empty-array-java/
Posted by: phillipsounins85.blogspot.com
0 Response to "Java How To Create Empty Array"
Post a Comment