-1

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?

Sam P
  • 453
  • 6
  • 19
  • Maybe add int varible like Int N =0; int[][] input = new int[101][N]; Or use ArrayList – Endiss Jun 05 '12 at 14:19
  • 1
    Duplicate. Use the search function please. http://stackoverflow.com/questions/2707357/how-to-create-dynamic-two-dimensional-array-in-java – Kim Jun 05 '12 at 14:21

6 Answers6

2

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?

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

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.

cDecker32
  • 813
  • 1
  • 10
  • 20
  • 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 05 '12 at 14:28
0

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.
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
0

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

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

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).

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
0

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.

whaley
  • 16,075
  • 10
  • 57
  • 68
0

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.

1337
  • 338
  • 1
  • 6