You are creating an array of zero length (no slots to put anything in)

 int array[]={/*nothing in here = array with no slots*/};

and then trying to assign values to array slots (which you don't have, because there are none)

array[i] = number; //array[i] = element i in the array of length 0

You need to define a larger array to fit your needs

 int array[] = new int[4]; //Create an array with 4 elements [0],[1],[2] and [3] each containing an int value
Answer from Ross Drew on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
You can also create an array by specifying its size with new. This makes an empty array with space for a fixed number of elements, which you can fill later:
Discussions

java - How to create an empty array? - Stack Overflow
Am new and I've searched the web and found it a bit frustrating, all I got was that in java array's can't be re-sized. I just want to figure out How do I create an empty array where I then prompt the More on stackoverflow.com
🌐 stackoverflow.com
How to declare and initialize an empty array of objects
I’m trying to declare an array of objects to add new objects later. I’m doing this away: private arrayOfObjects: [{}]; When I try to push some object to it I receive the following error: Cannot read property ‘push’ of undefined I believe this is because the array needs to be initialized. More on forum.ionicframework.com
🌐 forum.ionicframework.com
4
0
January 16, 2020
How to create empty array list without array size in java?
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends. More on sololearn.com
🌐 sololearn.com
3
1
Why is it good practice to declare an empty array and then push the items to that array?
Lucas Krause is having issues with: It's a good practice to declare products first as an empty array. What's the advantage over declaring and fil... More on teamtreehouse.com
🌐 teamtreehouse.com
2
September 21, 2014
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Memory for arrays is always allocated on the heap in Java. The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean) or null (for reference types). We can use array literals to initialize an array when declaring it. In this case, the new keyword is not required- ... The length of this array determines the length of the created array.
Published: October 8, 2026
🌐
Quora
quora.com › How-do-you-create-an-empty-array-in-Java
How to create an empty array in Java - Quora
Answer (1 of 2): Okay, so you can just do this.. . `ArrayList myList = new ArrayList ();` That's how I usually do it, right ? It's just , like , so simple . I mean , you could probably do it other ways but why bother? This is the easiest way, and honestly , I dont even think about it any...
🌐
Scaler
scaler.com › home › topics › check and declare empty array in java
Check and Declare Empty Array in Java - Scaler Topics
November 16, 2023 - If any item does not equal null then the boolean value isEmpty will be assigned false and we will break the loop. If isEmpty is false then our program will display "The input array is an empty array" otherwise it will display "The input array is not an empty array". In this section, we will learn how to check for a java empty array using the Apache commons library.
🌐
Baeldung
baeldung.com › home › java › java array › difference between null and empty array in java
Difference Between null and Empty Array in Java | Baeldung
August 10, 2024 - When we instantiate an empty array, Java allocates memory for the array structure but stores no elements. It’s important to note that when we create a non-empty array without specifying values for its elements, they default to zero-like values — 0 for an integer array, false for a boolean array, and null for an object array:
Find elsewhere
🌐
Pluralsight
pluralsight.com › blog › software development
How to initialize an array in Java | Pluralsight
When you make an array in Java for the first time, you’ve got to declare its size. This is because the array’s size is fixed from the moment you create it. The simplest way to do this is to initialize an empty array of a declared size.
🌐
Sentry
sentry.io › sentry answers › java › how do i declare and initialize an array in java?
Declare and Initialize Arrays in Java with Examples | Sentry
October 2, 2026 - Java will count the elements in our initial array and automatically initialize the array with enough memory for three ints. ... class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[]{1, 2, 3}; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } } ... If you don’t want to hard code the data immediately when you initialize the array, you can instead create an empty array (with all values initialized to zero) as follows.
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - We can either assign an array to null or an empty array: ... But we need to know the size when we initialize it because the Java virtual machine must reserve the contiguous memory block for it. As we discussed earlier, we can initialize an array with the new keyword: ... If we want to resize the array, we can do it by creating an array of larger size and copying the previous array elements to the new array:
🌐
Ionic Framework
forum.ionicframework.com › ionic framework › ionic-v3
How to declare and initialize an empty array of objects - ionic-v3 - Ionic Forum
January 16, 2020 - I’m trying to declare an array of objects to add new objects later. I’m doing this away: private arrayOfObjects: [{}]; When I try to push some object to it I receive the following error: Cannot read property ‘push’ of undefined I believe this is because the array needs to be initialized.
🌐
HappyCoders
happycoders.eu › home › java › how to declare and initialize arrays in java
How to Declare and Initialize Arrays in Java
September 24, 2026 - We do not yet specify the size of the array in the declaration. The array variables (in the example intArray and stringArray) do not contain the array itself but a reference to an array, which is ultimately an object on the Java heap. Only when we create the array object itself do we have to define its size.
🌐
Team Treehouse
teamtreehouse.com › community › why-is-it-good-practice-to-declare-an-empty-array-and-then-push-the-items-to-that-array
Why is it good practice to declare an empty array and then push the items to that array? (Example) | Treehouse Community
September 21, 2014 - What's the advantage over declaring and filling the array in one step? ... When you declare an empty array from the start, and THEN fill the array with data later on in the program, you prevent multiple errors from occurring because of a faulty array.
🌐
Java2Blog
java2blog.com › home › core java › java array › initialize empty array in java
How to initialize empty array in java? (with example) | Java2Blog
October 12, 2023 - In this post, we take a look on How to Initialize empty array in Java. We will look at different ways to Initialize arrays in Java with dummy values or with prompted user inputs. Arrays in Java follow a different declaration paradigm than other programming languages like C, C++, Python, etc. In Java, array is by default regarded as an object and they are allocated memory dynamically. Moreover, in java at the time of creation an array is initialized with a default value.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
July 21, 2026 - This class is a member of the Java Collections Framework. ... Constructs an empty list with the specified initial capacity. ... Constructs an empty list with an initial capacity of ten. ... Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. ... Trims the capacity of this ArrayList instance to be the list's current size.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-array
Java String Array: Declaration, Initialization & Examples | DigitalOcean
August 3, 2022 - Master Java String arrays with declaration, initialization, and manipulation examples. Learn iteration, sorting, searching, and conversion techniques.
🌐
CS Fundamentals
cs-fundamentals.com › java-programming › java-array-variables
Arrays in Java. Declare Initialize and Use Arrays in Java
Java allows creating an array of size zero. If the number of elements in a Java array is zero, the array is said to be empty. In this case you will not be able to store any element in the array; therefore the array will be empty.
🌐
Medium
medium.com › @rahul.tpointtech12 › java-create-empty-lists-a-step-by-step-guide-5c34f03228b3
Java Create Empty Lists: A Step-by-Step Guide | by Rahul | Medium
March 22, 2025 - So, the next time you’re working on a Java project and need to initialize an empty list, remember to leverage the power of ArrayList. ArrayList is a commonly used implementation of the List interface in Java. To create an empty ArrayList, follow these simple steps: