Two dimensional array initialization

  • Thread starter Thread starter Dadi
  • Start date Start date
D

Dadi

Hi,

I can make a simple initialization work like this:

Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};

But, now I want to create another array that consists of multiple copies of ONE_ROW like this (will not compile):

Object[,] TWO_ROWS = {ONE_ROW, ONE_ROW}; // Works in Java ;-)

This yields "Incorrectly structured array initializer"!!

This, however, is a correct implementation using hard-coded data:

Object[,] TWO_ROWS = {{"Vodafone", "5550160100197016"}, {"Vodafone", "5550160100197016"}};

Any ideas?

Regards,
Dadi.
 
Dadi said:
I can make a simple initialization work like this:

Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};

But, now I want to create another array that consists of multiple copies
of ONE_ROW like this (will not compile):

It sounds like you want jagged arrays then, rather than rectangular
arrays. If you do:

Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it'll work.

(Haven't tried it though...)
 
Actually Jon I think he wants

Object[][] TWO_ROWS =

a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
rectangular arrays which I don't think was the intent, wouldn't it?

I've been wrong about arrays before so maybe I am again.

Jon Skeet said:
Dadi said:
I can make a simple initialization work like this:

Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};

But, now I want to create another array that consists of multiple copies
of ONE_ROW like this (will not compile):

It sounds like you want jagged arrays then, rather than rectangular
arrays. If you do:

Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it'll work.

(Haven't tried it though...)
 
Daniel Billingsley said:
Actually Jon I think he wants

Object[][] TWO_ROWS =

a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
rectangular arrays which I don't think was the intent, wouldn't it?

I don't know - the intent wasn't very clear! However, my code would
give a single array of 2D rectangular arrays.
 
That's what I said, isn't it? :)

I meant your overall array [][,] would be jagged, as opposed to simply 3D
[,,].

Yes, no, maybe so? (Trying to see if I'm starting to grasp this terminology
as well as I think I am.)
 
Daniel Billingsley said:
That's what I said, isn't it? :)

Not quite - it's definitely *not* a jagged array *of* 2D rectangular
arrays, in the same way that int[] isn't a jagged array of ints, it's
just an array of ints, if you see what I mean.
I meant your overall array [][,] would be jagged, as opposed to simply 3D
[,,].

I'm not sure whether I'd say the overall array is jagged or not - it's
certainly not rectangular, but it's not jagged in the normal sense
either.

Fortunately this is unlikely to actually be an issue very often :)
 
Hmm... I thought jagged simply meant an array of arrays... what did you mean
by the "normal sense" of jagged usage?

an array of ints is 1 dimensional, and not relevant to this discussion in my
mind.

an array of arrays, or an array of arrays of arrays, or an array of 2D
arrays, are all "jagged" I would think by definition, no?

[,] defines 2D array, definitely not jagged
[][] defines array of arrays, definitely jagged

[][,] defines an array of 2D arrays, jagged, right?
[][][] defines an array of arrays of arrays, jagged, right?
[,,] defines a 3D array, not jagged, right?

Jon Skeet said:
Daniel Billingsley said:
That's what I said, isn't it? :)

Not quite - it's definitely *not* a jagged array *of* 2D rectangular
arrays, in the same way that int[] isn't a jagged array of ints, it's
just an array of ints, if you see what I mean.
I meant your overall array [][,] would be jagged, as opposed to simply 3D
[,,].

I'm not sure whether I'd say the overall array is jagged or not - it's
certainly not rectangular, but it's not jagged in the normal sense
either.

Fortunately this is unlikely to actually be an issue very often :)
 
Daniel Billingsley said:
Hmm... I thought jagged simply meant an array of arrays... what did you mean
by the "normal sense" of jagged usage?

I'd say that a jagged array of "X"s is an array of arrays of "X"s.
an array of ints is 1 dimensional, and not relevant to this discussion in my
mind.

Ah, but it is in mine. Here's a jagged array of ints:

int[][] x;

That means a jagged array *of* arrays is similarly:

Something[][][] x;

That's a jagged array of (Something[]).

Just

Something[][] x;

is a jagged array of (Something) instead.

In this case we have:

Something[][,];

which is a one-dimensional array of (Something[,]), rather than a
jagged array of Something[,]. It may or may not (see below) count as a
jagged array of Something. Semi-jagged perhaps?
an array of arrays, or an array of arrays of arrays, or an array of 2D
arrays, are all "jagged" I would think by definition, no?

Not sure. Possibly, possibly not. My main point was that it wasn't a
jagged array of 2D arrays, but now we're getting into deeper water :)

I keep flip-flopping on whether or not I'd consider it jagged in
general. I think I probably would, on balance, contrary to my previous
post.
 
I'm sure where you landed when you flip-flopped, so I don't know if we're
still disagreeing. :)

Jon Skeet said:
I'd say that a jagged array of "X"s is an array of arrays of "X"s.

Yes I agree. MS defines a jagged array as "an array whose elements are
arrays." But there is no specification of what that second "of arrays"
looks like - 1D, 2D, 3D doesn't change the definition.

Something[][,];

which is a one-dimensional array of (Something[,]), rather than a
jagged array of Something[,]. It may or may not (see below) count as a
jagged array of Something. Semi-jagged perhaps?

Is Something[,] not an array? Then that makes Something[][,] an
array of arrays, and therefore jagged by definition, just as MS explains a
bit further down on that same doc page:

"It is possible to mix jagged and multidimensional arrays. The following is
a declaration and initialization of a single-dimensional jagged array that
contains two-dimensional array elements of different sizes:

int [] [,] myJaggedArray = new int [] [3,] { new int [,] " blah blah blah
Not sure. Possibly, possibly not. My main point was that it wasn't a
jagged array of 2D arrays, but now we're getting into deeper water :)

Sure looks to me like MS just said [][,] is a jagged array of 2D arrays.

I'm sorry, I'm not trying to be a smart alec proving you wrong per se.. it's
just that I find myself completely confused and wrong enough that when I
think I've got something nailed for once I've got to cling to it
desperately. LOL.
 
I meant I'm *NOT* sure, of course. I've been typing like a retard all day.
Need coffee.
 
Back
Top