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
🌐
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.
Discussions

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
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
java - How can I check whether an array is null / empty? - Stack Overflow
I have an int array which has no elements and I'm trying to check whether it's empty. For example, why is the condition of the if-statement in the code below never true? int[] k = new int[3]; if... More on stackoverflow.com
🌐 stackoverflow.com
FAQ: Learn Java: Arrays - Creating an Empty Array
This community-built FAQ covers the “Creating an Empty Array” exercise from the lesson “Learn Java: Arrays”. Paths and Courses This exercise can be found in the following Codecademy content: Learn Java FAQs on the exercise Creating an Empty Array There are currently no frequently asked ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
November 15, 2019
🌐
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:
🌐
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...
🌐
Delft Stack
delftstack.com › home › howto › java › return empty array java
How to Return Empty Array in Java | Delft Stack
February 2, 2024 - After we ran the program, it printed the length of the empty array, which is 0. Empty curly braces, { }, in Java, represent an empty array or collection.
Find elsewhere
🌐
Java2Blog
java2blog.com › home › core java › java array › return empty array in java
How to return an empty array in java? (with example) | Java2Blog
October 11, 2023 - Now, let us look into the steps. The ArrayUtils class has a static field EMPTY_STRING_ARRAY which returns a empty String array. It has similar other static fields which return int, char, float etc.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-array-empty-check
Java Array Empty Check - GeeksforGeeks
July 23, 2025 - In Java, an array is considered non-empty, if it is not null and its length is greater than 0.
🌐
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 - An empty array in Java is an array that has been instantiated but contains zero elements. This means it’s a valid array object and can be used in operations, although it doesn’t hold any values.
🌐
Coderanch
coderanch.com › t › 391225 › java › empty-array-declarations
empty array declarations (Beginning Java forum at Coderanch)
Why, is declaring an array like this: int[] numbers = null; versus declaring an array like this: int[] numbers = new int[]; (which is not allowed), conceptually different than declaring a two dimesional array like this: int[][] numbers = {null, null}; versus like this: int[][] numbers = new ...
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › java faq
FAQ: Learn Java: Arrays - Creating an Empty Array - Java FAQ - Codecademy Forums
November 15, 2019 - This community-built FAQ covers the “Creating an Empty Array” exercise from the lesson “Learn Java: Arrays”. Paths and Courses This exercise can be found in the following Codecademy content: Learn Java FAQs on th…
🌐
CS Fundamentals
cs-fundamentals.com › java-programming › java-array-variables
Arrays in Java. Declare Initialize and Use Arrays in Java
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.
🌐
Baeldung
baeldung.com › home › java › java array › checking if an array is null or empty in java
Checking if an Array Is Null or Empty in Java | Baeldung
July 16, 2024 - Checking if an array is null or empty is a fundamental task in Java programming. In this article, we’ve explored how to implement these checks for both object and primitive type arrays, creating a robust utility method that can be reused in our applications.
🌐
Linux Hint
linuxhint.com › initialize-empty-array-java
Linux Hint – Linux Hint
September 1, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Quora
quora.com › How-do-I-empty-an-array-in-Java
How to empty an array in Java - Quora
Answer (1 of 5): Depends on how you define “empty”. An array is a fixed length array of elements of a specific data type. Assuming it is an array of objects and not of primitive types, the initial values of all of the elements when you first create the array are [code ]null[/code] unless ...
🌐
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.