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
Discussions

Empty an array in Java / processing - Stack Overflow
Other than looping through each element in an array and setting each one to null, is there a native function in Java / processing to simply empty an array (or destroy it, to be able to redeclare it... More on stackoverflow.com
🌐 stackoverflow.com
Empty Array
You are obviously talking about two different things. Arrays have a predetermined length that can be checked with .length Checking the fill state of an array is a tad trickier. You will need to keep track of the filled elements by yourself. You can do this by keeping the index of the last added element. Java also knows something similar to dynamic arrays (Lists in Python), namely ArrayList. More on reddit.com
🌐 r/learnprogramming
11
1
January 27, 2022
How to create an empty array with a lenght determined by another method ?
int[] sum = new int[method(x)] More on reddit.com
🌐 r/javahelp
6
4
December 1, 2015
[Java] Why can't we create an array of type T?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
11
8
July 30, 2023
🌐
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:
🌐
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.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "java howtos" › "how to initialize an empty array in java"
How to Initialize an Empty Array in Java | Delft Stack
February 2, 2024 - Arrays are like containers that allow you to store and organize multiple values of the same type in a single variable. In Java, initializing an empty array involves using the new keyword, and there are several ways to achieve this.
🌐
Techie Delight
techiedelight.com › home › java › declare an empty array in java
Declare an empty array in Java | Techie Delight
July 7, 2026 - This post will discuss how to declare an empty array in Java. To create an empty array, you can use an array initializer. The length of the array is equal to the number of items enclosed within the braces of the array initializer.
🌐
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.
Find elsewhere
🌐
javaspring
javaspring.net › blog › java-create-empty-array
Java Create Empty Array: A Comprehensive Guide — javaspring.net
Arrays in Java can hold primitive data types (such as int, double, boolean) or objects. When creating an empty array of primitive types, the elements are initialized to their default values (e.g., 0 for numeric primitives, false for boolean).
🌐
javaspring
javaspring.net › blog › create-an-empty-array-java
Creating an Empty Array in Java: A Comprehensive Guide — javaspring.net
An array in Java is an object that stores a fixed number of values of a single type. When you create an empty array, you are essentially allocating memory for a specific number of elements of a particular type, but those elements are not initialized with any values yet.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "java howtos" › "how to create an empty array in java"
How to Create An Empty Array in Java | Delft Stack
March 11, 2025 - This method is particularly useful ... any data. Creating a null array in Java is quite simple and is done by declaring the array without initializing it....
🌐
Javatpoint
javatpoint.com › how-to-declare-an-empty-array-in-java
How to Declare an Empty Array in Java - Javatpoint
How to Declare an Empty Array in Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Mccue
javabook.mccue.dev › arrays › empty_array
Empty Array - Modern Java
If you use an array initializer that has no elements between the { and } you can create an empty array.
🌐
Codecademy
external.codecademy.com › courses › java-loops-and-arrays › lessons › learn-java-arrays › exercises › creating-an-empty-array
| Codecademy
Learn Java: Arrays · Creating an Empty Array · We can also create empty arrays and then fill the items one by one. Empty arrays have to be initialized with a fixed size: String[] menuItems = new String[5]; Once you declare this size, it cannot ...
🌐
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.
🌐
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.
🌐
KoderHQ
koderhq.com › tutorial › java › array
Java Arrays Tutorial | KoderHQ
Then, we give the array a name and postfix it with open and close square brackets. The brackets indicate to the compiler that we’re declaring an array, not a variable. ... This will create an empty array that we can add values to.
🌐
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.
🌐
javaspring
javaspring.net › blog › initialize-empty-array-java
Initializing Empty Arrays in Java — javaspring.net
The elements are accessed using ... classes). The most common way to initialize an empty array is by first declaring the array and then specifying its size....
🌐
Delft Stack
delftstack.com › home › howto › java › return empty array java
How to Return Empty Array in Java | Delft Stack
February 2, 2024 - We have explored various methods to return an empty array in Java. The new int[0] and empty curly braces {} methods provide a direct and straightforward way to represent an array without any elements.