1

I'm sorry to ask such a basic question, but I cannot see by myself where my code breaks. I am trying to define a two-dimensional array of objects, and as far as I can tell the code is failing to register the second dimension. Later code is accordingly unable to designate a two-dimensional index within the array, as the second dimension exists only as a null quantity.

I'm feeling pretty stupid here. I have to imagine my error is something glaringly basic as there is not enough code involved for it to be nuanced, and yet I cannot see it! I'd appreciate any help you could give me to focus my apparently blind eyes on the problem.

Here is my code:

int x = 17;
Object[][] 2Darr = new Object[50][x];

Running this code yields an array defined as [50][].

Bibliophael
  • 177
  • 11

3 Answers3

1

There are no as such two dimensional arrays in java. There are only arrays of arrays . What you can do is

Object[][] arr = new Object[50][];
arr[0] = new Object[5]; // zeroth element of arr with value as an array of 5 elements
arr[1] = new Object[7]; // first element of arr with value as an array of 7 elements
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • So I gather, and yet I understand the distinction is practically one of semantics. – Bibliophael Nov 28 '13 at 02:28
  • To clarify, I was under the impression that one may define a 2D array in Java by defining two array-lengths in the first initialization. As User 2310289 demonstrates below. – Bibliophael Nov 28 '13 at 02:37
  • If you could spare a moment to clarify your statement, I would appreciate it. – Bibliophael Nov 28 '13 at 02:51
  • @Bibliophael, sorry for late reply. Actually, what I meant to say is that when there are multidimensional arrays, they are actually arrays of arrays in java. That was my point. You have defined a 2D arrray that is fine. What I'm confused with is the same as your concern. Why is the same thing giving different output to you and another user. I'm looking into this now. – Prasad Kharkar Nov 28 '13 at 02:56
  • I'm sorry if I sounded impatient in my earlier comment. I've been experimenting with the code and I find that an explicit definition of the second dimension fails as well. That is, stepping through each index of the primary array and attempting to designate to each one an array of certain length does not work. The debugger spins into a series of unfound paths and crashes the program. – Bibliophael Nov 28 '13 at 03:11
1

If I run this in Eclipse

int six = 6;
String [][] arr = new String [5][six];

And then look at the array in a debugger I see:

[[null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null], [null, null, null, null, null, null]]

So I think this works as I expect.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You don't need int x=17; below code is enough to create 2 dimensional array in java. Object[][] 2Darr = new Object[50][]; This could be probable duplicate of Syntax for creating a two-dimensional array

Community
  • 1
  • 1
Kishore
  • 300
  • 3
  • 14
  • The link you provide asserts that I may define a 2D array by code similar in form to this: int[][] multi = new int[5][10]; Which is how I would expect my given code to perform. – Bibliophael Nov 28 '13 at 02:38