Take the 2-minute tour ×

Just a quick question - if I instantiate a new two dimensional int array like so

int[][] input = new int[101][]; //number is irrelevant

eclipse seems to not mind this, but I'm not entirely sure if this would work like I'm expecting it to, which is that the second part of the array would act dynamically.

after reading your answers, I realise that my question is badly worded.

I am reading in from a file - it is a CSV and there is 101 columns and X amount of rows. Since I am reading the file in line-by-line, it is simple for me to figure out that the first dimension of my "2-d array" is 101. But, also because I am reading the file line-by-line, I cannot say for certain that my second dimension will be X without iterating through the entire file.

How would I find X using my current method, or is that actually impossible?

share|improve this question
Maybe add int varible like Int N =0; int[][] input = new int[101][N]; Or use ArrayList –  Endiss Jun 5 '12 at 14:19
Duplicate. Use the search function please. stackoverflow.com/questions/2707357/… –  Kim Jun 5 '12 at 14:21
add comment

6 Answers

Then you'd have 101 elements in input which are all null. To create the second dimension, you'd need to initialize it for every item:

input[13] = new int[63];
input[14] = new int[128];

The second dimension doesn't need to be the same for all first level items. The second dimension would be the same if you used this:

int[][] input = new int[101][50];

All values would then be initialized to 0.

But the real answer is: Why didn't you just try?

share|improve this answer
add comment

As the other answers have pointed out, Java only has "jagged" arrays - arrays of arrays in other words.


What your code is doing here is creating an 101-element array of int[]s. Since int[] is an Object, each element is initialized to null. You would then need to set each int[] element before being able to use it:

input[0] = new int[4];

There is a shortcut syntax that let's you quickly initialize the inner arrays all to one size:

int[][] input = new int[101][4];

This would create a 101-element array of 4-element arrays of int (initialized to 0).

share|improve this answer
add comment

Eclipse does not mind that you have not initialized the second dimension. In your approach you should initialize every sub array: int[1], int[2], etc.

share|improve this answer
add comment

Technically, Java does not have the concept of "two dimensional" arrays. What it does have, and what you have written, is an array of arrays - which allows you to have jagged arrays.

By writing something new int[3][], you've basically said I have an array of arrays that you can conceptualize as [null,null,null] because you haven't initialized any of the actually "inner" arrays. At this point you need to do something like

input[0] = new int[3]
input[1] = new int[3]
input[2] = new int[3]

which you can then conceptualize as [[0,0,0],[0,0,0],[0,0,0]].

Writing new int[3][3] accomplishes this same goal.

share|improve this answer
add comment

Java supports only jagged arrays, which means all dimensions are not initialized automagically, but you have to initialize them yourself:

for (int i = 0; i < input.length; ++i)
    input[i] = new int[42]; // or anything else.
share|improve this answer
add comment

I'm guessing you're looking for that second dimension to be variable in length, in which case you might as well just make it an array of arrays, thus giving you the ability to define the length of each row.

share|improve this answer
In what way isn't it an array of arrays in his/her code? –  esej Jun 5 '12 at 14:24
ok yes it IS an array of arrays, but he should declare each array within the array individually instead of using the default 2d array constructor, which applies the same dimension to all the arrays. –  cDecker32 Jun 5 '12 at 14:28
add comment

Your Answer


