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 - In this section, we will learn how to check for a java empty array using the NULL property. An array can be considered an empty array java if the array displays the value NULL. ... In the above example, we have declared an array dates and assigned the values null.
Discussions

java - How to create an empty array? - Stack Overflow
35 I want to declare an empty array in Java and then I want do update it but the code is not working More on stackoverflow.com
🌐 stackoverflow.com
April 15, 2014
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
Can we declare empty arrays in Java? - Stack Overflow
How to declare empty array so that I can enter any number of elements I want. 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
🌐
Delft Stack
delftstack.com › home › howto › java › initialize empty array java
How to Initialize an Empty Array in Java | Delft Stack
February 2, 2024 - As you can see, we first declare an empty array named array with size 0 using int array[] = new int[0];. This syntax signals that the array is initially empty, and no memory is allocated for elements.
🌐
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 - Considering all cases a user must face, these are the different methods to initialize an array: Let us look at these methods in detail. To declare empty array, we can use empty array initializer with {}. Java
🌐
Techie Delight
techiedelight.com › home › java › declare an empty array in java
Declare an empty array in Java | Techie Delight
July 7, 2026 - Java allows an empty array initializer, in which case the array is said to be empty. The following code creates an array of zero length using an empty array initializer. Note that once an array is created, its length never changes. If you’re looking for the resizable arrays, use an ArrayList instead. ... You can also use an array creation expression to create an empty array. ... You can declare a two-dimensional array in Java using a similar syntax.
🌐
Coderanch
coderanch.com › t › 391225 › java › empty-array-declarations
empty array declarations (Beginning Java forum at Coderanch)
In the cases of the single dimension array declaration and the two dimensional array declarations, all declare arrays with components that are null. Not being able to use all of the above styles would seem to me to be an inconsistent grammar in Java. Perhaps it isn't and I'm curious about what is going on with regards to memory allocation (or any other related concern) that this is not an inconsistent grammar in Java?
🌐
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...
Find elsewhere
🌐
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.
🌐
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
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Use new with a size when you want to create an empty array and fill it later. ... Coding fundamentals as a game. Bite-sized lessons and challenges. ... Ready to start your journey? Your streak is waiting. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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
Java also supports empty arrays, and even negative size arrays, however, empty arrays cannot be used to store elements. Java provides a special syntax of for loop called enhanced for loop or for-each to access Java array elements.
🌐
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 - We can create an array or just return this value : new int[0] that is an empty array of int . Let us take a look at the example code. ... This is another alternative way to declare an empty array with no dimensions i.e. size and then return it.
🌐
Reddit
reddit.com › r/javahelp › is it possible to declare a blank array with multiple spaces?
r/javahelp on Reddit: Is it possible to declare a blank array with multiple spaces?
July 13, 2020 -

I'm trying to write a method that involves taking an int and using that number to create an array of that size. However, the contents of the array are not known at initialization. Is there a way to declare an array with (for example) ten blank spaces?

I tried

int[] MyArray = new int[Num];
MyArray[Num] = Num; 

but that only creates an array with Num at the first index.

Thanks for any help, I really appreciate it :)

🌐
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....
🌐
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.
🌐
Career Karma
careerkarma.com › blog › java › java initialize array: a step-by-step guide
Java Initialize Array: A Step-By-Step Guide | Career Karma
December 1, 2023 - Before you use this approach, you first need to declare an array. So, if we wanted to declare an empty array called bagelFlavors, we would use the code like we did above: