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][].

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

5 Comments

So I gather, and yet I understand the distinction is practically one of semantics.
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.
If you could spare a moment to clarify your statement, I would appreciate it.
@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.
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.
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.

5 Comments

That is what I would expect, certainly, and yet it is not what my program yields.
How are you testing it?
The Eclipse Debugger. I would expect to see something like what you listed above, but when I look at the array I only see one dimension, or rather one vertical list of undefined arrays.
I wonder why my Eclipse Debugger and your are different? Are you looking at / loading a different class / code ?
The object class held by the array is one I have defined myself. It's basically a simple skin for an ArrayList. Would that cause a problem?
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.