You should use a List for something like this, not an array. As a general rule of thumb, when you don't know how many elements you will add to an array before hand, use a List instead. Most would probably tackle this problem by using an ArrayList.

If you really can't use a List, then you'll probably have to use an array of some initial size (maybe 10?) and keep track of your array capacity versus how many elements you're adding, and copy the elements to a new, larger array if you run out of room (this is essentially what ArrayList does internally). Also note that, in the real world, you would never do it this way - you would use one of the standard classes that are made specifically for cases like this, such as ArrayList.

Answer from arshajii on Stack Overflow
Discussions

Java: init array -- but don't know size - Get Started - SitePoint Forums | Web Development & Design Community
how do I decl/init an array of strings in Java when I don’t know the size of it (it’s coming from request… not from a form… it’s sent from ajax…) thank you… More on sitepoint.com
🌐 sitepoint.com
0
May 24, 2014
Declare an array in java without size - Stack Overflow
Hello am trying to declare an array in java but i do not want the array to have a specific size because each time the size must be different. I used this declaration: int[] myarray5; but when... More on stackoverflow.com
🌐 stackoverflow.com
java - How can I initialize an array without knowing it size? - Stack Overflow
I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria. Now problem is I do not... More on stackoverflow.com
🌐 stackoverflow.com
java - declare an array with unknown size without using ArrayList - Stack Overflow
Is there a way to declare an array with an unknown length? My problem is to return an int[] of odd integers from a range of numbers. My current output is adding 0s to fill up the remaining space of... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Significservices
significservices.com › uwqcj › how-to-initialize-an-array-in-java-with-unknown-size.html
how to initialize an array in java with unknown size
To instantiate an array, use this syntax: arrayName = new datatype[ size ]; where size is an expression that evaluates to an integer and specifies the number of elements.When an array is instantiated, the elements are assigned default values according to the array data type..
🌐
Quora
quora.com › How-can-we-create-array-without-specifying-a-size-in-java
How can we create array without specifying a size in java? - Quora
Answer (1 of 11): Yes. You can, technically, declare the type of a variable as an array (of specific type) like int[ ] a; But you can't use it before initializing it. This array ‘a’ doesn't exist anywhere in the memory. The compiler only knows that ‘a’ will be an array.
🌐
Scholarsnest
scholarsnest.com › dr-brown › how-to-initialize-an-array-in-java-with-unknown-size
how to initialize an array in java with unknown size
Above that each of the array starts with index zero to declare Java array with zeros you can using! Block of memory where each location is a two-dimensional array: so basically! Of other technologies set to zero ( by the two methods ; by specifying the size intelliJIDEA to... Utf-32 Charac how to initialize an unknown array example Tutorial have to process it again a contiguous of!
🌐
Baeldung
baeldung.com › home › java › java array › initializing arrays in java
Initializing Arrays in Java | Baeldung
December 16, 2024 - Here, the fill() method accepts the initialized array, the index to start the filling, the index to end the filling (exclusive), and the value itself respectively as arguments. The first three elements of the array are set to -50 and the remaining ...
🌐
javaspring
javaspring.net › blog › java-how-do-i-initialize-an-array-size-if-it-s-unknown
Java: How to Initialize an Array with Unknown Size for User Input Numbers (1-100) — javaspring.net
Java arrays require a predefined size at initialization. For example: int[] numbers = new int[5]; // Fixed size: holds exactly 5 elements · If the user enters more than 5 numbers, the array will overflow; if fewer, memory is wasted. In contrast, dynamic data structures (like ArrayList) grow or shrink automatically. This makes them ideal for scenarios where input size is unknown (e.g., user input until “quit” is entered).
Find elsewhere
🌐
Behindthewash
behindthewash.com › cullman-daily › how-to-initialize-an-array-in-java-with-unknown-size
how to initialize an array in java with unknown size
Its elements known at compile time 3 ) using pointer to a method Java! Values, unlike ArrayList, which can only hold object values and we want to 2D. = value/element to initialize an array in C or C++ dont initialize it hold values. Initialize array with Values/Elements an unknown quantity of data to be stored the. Once declared starting size of an array ArrayList instead, it is zero than other types print!
🌐
SitePoint
sitepoint.com › get started
Java: init array -- but don't know size - Get Started - SitePoint Forums | Web Development & Design Community
May 24, 2014 - how do I decl/init an array of strings in Java when I don’t know the size of it (it’s coming from request… not from a form… it’s sent from ajax…) thank you…
🌐
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 - Answer: Yes. We can declare an array without size but before using it needs to be initialized. ... Answer: Yes. Arrays are static in Java and you declare an array with a specified size.
🌐
Stack Abuse
stackabuse.com › how-to-declare-and-initialize-an-array-in-java
How to Declare and Initialize an Array in Java
September 20, 2022 - Note that we've only created an array reference. No memory has been allocated to the array as the size is unknown, and we can't do much with it. To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size:
🌐
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.
🌐
Coderanch
coderanch.com › t › 433247 › java › create-array-unknown-size-add
Is it possible to create an array of unknown size, and add data to it? (Beginning Java forum at Coderanch)
Never mind, I actually figured out how to solve my own problem efficiently - at least as efficiently as I can think of right now. I know how many entries (lines of text) there are because that's the first line in the text file, so I can make one array that length.
🌐
Reddit
reddit.com › r/cprogramming › how to create an array with unknown size?
r/cprogramming on Reddit: How to Create an array with unknown size?
March 11, 2020 -

I have participated in many coding competitions but I'm unable to pass all the test cases just because of this problem. I don't know how to initialize an array with unknown size. I tried using

arr=malloc(size*sizeof(int));

But at the end it shows segmentation fault. Can someone explain why?

🌐
LabEx
labex.io › tutorials › java-how-to-create-java-array-with-predefined-size-418027
How to create Java array with predefined size | LabEx
Learn essential techniques for creating and initializing fixed-size arrays in Java, with practical examples and best practices for array management and manipulation.
🌐
Quora
quora.com › How-can-I-declare-memory-space-for-an-array-in-Java-when-I-dont-know-the-size-At-the-time-of-storing-the-elements-the-size-of-array-is-unknown
How to declare memory space for an array in Java when I don't know the size - Quora
Answer (1 of 3): Actually you cannot. Arrays are contiguous data structures i.e the space occupied in memory is contiguous and that the address of the next array element can be calculated from the current element. Hence when you declare an array, compiler needs the size of array, so that the spa...