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
🌐
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.
🌐
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.
🌐
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 ...
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › 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.
🌐
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.
🌐
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..
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
March 29, 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…
🌐
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:
🌐
Sentry
sentry.io › sentry answers › java › how to initialize an array in java?
How to initialize an array in Java? | Sentry
In Java, the array object is used to store multiple elements of the same type. These elements are stored in contiguous memory, the length of which is immutable once initialization is complete.
🌐
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!
🌐
JanBask Training
janbasktraining.com › community › java › how-do-i-declare-and-initialize-an-array-in-java
How do I declare and initialize an array in Java? | JanBask Training Community
May 21, 2025 - To declare an array in Java, you specify the type of elements it will hold, followed by square brackets. ... This line declares an array of integers but doesn’t allocate memory yet. ... If you know how many elements the array will hold but not their values yet, you can initialize it with a fixed size.
Top answer
1 of 16
3263

You can either use array declaration or array literal (but only when you declare and affect the variable right away, array literals cannot be used for re-assigning an array).

For primitive types:

int[] myIntArray = new int[3]; // each element of the array is initialised to 0
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

// Since Java 8. Doc of IntStream: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html

int [] myIntArray = IntStream.range(0, 100).toArray(); // From 0 to 99
int [] myIntArray = IntStream.rangeClosed(0, 100).toArray(); // From 0 to 100
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).toArray(); // The order is preserved.
int [] myIntArray = IntStream.of(12,25,36,85,28,96,47).sorted().toArray(); // Sort 

For classes, for example String, it's the same:

String[] myStringArray = new String[3]; // each element is initialised to null
String[] myStringArray = {"a", "b", "c"};
String[] myStringArray = new String[]{"a", "b", "c"};

The third way of initializing is useful when you declare an array first and then initialize it, pass an array as a function argument, or return an array. The explicit type is required.

String[] myStringArray;
myStringArray = new String[]{"a", "b", "c"};
2 of 16
336

There are two types of array.

One Dimensional Array

Syntax for default values:

int[] num = new int[5];

Or (less preferred)

int num[] = new int[5];

Syntax with values given (variable/field initialization):

int[] num = {1,2,3,4,5};

Or (less preferred)

int num[] = {1, 2, 3, 4, 5};

Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise no difference. Not at all.

Multidimensional array

Declaration

int[][] num = new int[5][2];

Or

int num[][] = new int[5][2];

Or

int[] num[] = new int[5][2];

Initialization

 num[0][0]=1;
 num[0][1]=2;
 num[1][0]=1;
 num[1][1]=2;
 num[2][0]=1;
 num[2][1]=2;
 num[3][0]=1;
 num[3][1]=2;
 num[4][0]=1;
 num[4][1]=2;

Or

 int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

Ragged Array (or Non-rectangular Array)

 int[][] num = new int[5][];
 num[0] = new int[1];
 num[1] = new int[5];
 num[2] = new int[2];
 num[3] = new int[3];

So here we are defining columns explicitly.
Another Way:

int[][] num={ {1}, {1,2}, {1,2,3,4,5}, {1,2}, {1,2,3} };

For Accessing:

for (int i=0; i<(num.length); i++ ) {
    for (int j=0;j<num[i].length;j++)
        System.out.println(num[i][j]);
}

Alternatively:

for (int[] a : num) {
  for (int i : a) {
    System.out.println(i);
  }
}

Ragged arrays are multidimensional arrays.
For explanation see multidimensional array detail at the official java tutorials

🌐
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?
April 26, 2019 -

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?