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

Is it possible to declare a blank array with multiple spaces?
Thanks for the help! After a little more digging, I found what I needed in this SO post: https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array More on reddit.com
🌐 r/javahelp
5
9
July 13, 2020
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 can I initialize a String array with length 0 in Java? - Stack Overflow
It's clearer to me that it means "I want a string array with 0 elements" rather than "I want an array with this content - which is empty". Just personal preference I guess. 2009-11-03T08:40:19.48Z+00:00 ... @Tony - I have to use the few places where Java can infer a type. More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
You 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: May 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...
🌐
Sentry
sentry.io › sentry answers › java › how do i declare and initialize an array in java?
How do I declare and initialize an array in Java? | Sentry
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 - When we declare an array, knowing the size is unnecessary. 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.
Find elsewhere
🌐
Scaler
scaler.com › home › topics › check and declare empty array in java
Check and Declare Empty Array in Java - Scaler Topics
November 16, 2023 - To initialize an empty array java we need to use the new keyword followed by the data type of the elements the array would store.
🌐
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 :)

🌐
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.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
JavaScript does not have a special runtime value for integers, so there’s no equivalent to int or float - everything is simply number ... The type names String, Number, and Boolean (starting with capital letters) are legal, but refer to some special built-in types that will very rarely appear in your code. Always use string, number, or boolean for types. To specify the type of an array like [1, 2, 3], you can use the syntax number[]; this syntax works for any type (e.g.
🌐
CS 61B Spring 2026
sp26.datastructur.es › proj5 › proj5b
Project 5B: BYOW (World Generation) | CS 61B Spring 2026
Initializing the tile rendering engine. Generating a two dimensional TETile[][] array. Using the tile rendering engine to display the TETile[][] array. For empty tiles, make sure to use Tileset.NOTHING, the pure black tile.
🌐
College Board
apcentral.collegeboard.org › media › pdf › ap25-frq-computer-science-a.pdf pdf
2025 AP ® Computer Science A Free-Response Questions
/** Initializes competitorList, as described in part (a) */ ... B. Write the Round method buildMatches. This method should return a new · ArrayList<Match> object by pairing competitors from competitorList according to the
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-check-if-an-array-is-empty-or-not-in-java
How to Check if an Array is Empty or Not in Java
August 6, 2024 - Step 3 ? If length is equal to 0 then array is empty otherwise not. Step 4 ? Print the desired result. To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
🌐
Go
go.dev › doc › effective_go
Effective Go - The Go Programming Language
The built-in function make(T, args) serves a purpose different from new(T). It creates slices, maps, and channels only, and it returns an initialized (not zeroed) value of type T (not *T). The reason for the distinction is that these three types represent, under the covers, references to data structures that must be initialized before use. A slice, for example, is a three-item descriptor containing a pointer to the data (inside an array), the length, and the capacity, and until those items are initialized, the slice is nil.
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array – declare, create & initialize an array in java
Java Array - Declare, Create & Initialize An Array In Java
April 1, 2025 - Here, as you can see we have initialized the array using for loop. Each element ‘i’ of the array is initialized with value = i+1. Apart from using the above method to initialize arrays, you can also make use of some of the methods of ‘Arrays’ class of ‘java.util’ package to provide initial values for the array.
🌐
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 - A null array in Java indicates that the array reference doesn’t point to any object in memory. Java initializes reference variables, including arrays, to null by default unless we explicitly assign a value.
🌐
Coderanch
coderanch.com › t › 714900 › java › Array-empty-braces-size-String
Array with empty braces and without size : String[] lion ...
August 20, 2019 - Hi Campbell Ritchie, I'm new here and just started to prepare for OCA. I would like to try to answer questions, please correct me if im wrong. 1: not possible to declare the length and initialize array at the same time. 3: you will get out of bound exception - array is initialized but is empty (no elemment on position 0).
🌐
Medium
medium.com › @shenithbandara › declaring-and-initializing-java-arrays-8df287500653
Declaring and Initializing Java Arrays | by Shenithbandara | Medium
October 7, 2025 - A universal truth in Java (and many languages): array indexing starts at 0. If an array has N elements, the valid indices range from 0 to N−1. ... Once an array is created, its size is fixed. You cannot change an array’s length after allocation. If you need a resizable collection, you should use a class like ArrayList. Let's discuss the declaration and initialization of arrays.