jagged array initialization


F

Frank Buss

I'm porting some JavaScript code to C# (and I'm new to C#). This is the
beginning of the JavaScript code:

var pos = [
[ [ 0, 0 ] ],
[ [ -10, 0 ], [ 10, 0 ] ],
[ [ -10, -7 ], [ 12, -7 ], [ 0, 7 ] ],
....

I managed to write it like this for C# :

int[][][] pos = new int[][][]{
new int[][] { new int[] { 0, 0 } },
new int[][] { new int[] { -10, 0 }, new int[] { 10, 0 } },
new int[][] { new int[] { -10, -7 }, new int[] { 12, -7 }, new int[] {
0, 7 } },
....

but this looks very ugly and unreadable. Is there a nicer way to write
it in C#, like in JavaScript?
 
Ad

Advertisements

A

Arne Vajhøj

int[][][] pos = new int[][][]{
new int[][] { new int[] { 0, 0 } },
new int[][] { new int[] { -10, 0 }, new int[] { 10, 0 } },
new int[][] { new int[] { -10, -7 }, new int[] { 12, -7 }, new int[] {
0, 7 } },
For a multi dimensional arrays you just use nested {} without any new.

Jagged arrays are more tricky. You can omit the new in the outermost and
you can omit the type in the innermost. The rest is needed.

int[][][] pos = { new int[][] { new [] { 0, 0 } },
new int[][] { new [] { -10, 0 }, new [] {
10, 0 } },
new int[][] { new [] { -10, -7 }, new []
{ 12, -7 }, new [] { 0, 7 } } };

Arne
 
F

Frank Buss

Am 24.12.2014 um 01:22 schrieb Arne Vajhøj:
Jagged arrays are more tricky. You can omit the new in the outermost and
you can omit the type in the innermost. The rest is needed.

int[][][] pos = { new int[][] { new [] { 0, 0 } },
new int[][] { new [] { -10, 0 }, new [] {
10, 0 } },
new int[][] { new [] { -10, -7 }, new [] {
12, -7 }, new [] { 0, 7 } } };
Still ugly. I found a trick on this webpage, mixing jagged and
multidimensional arrays:

http://www.csharphelp.com/2007/09/c-simple-rectangular-mixed-and-jagged-arrays-foreach-loops-and-nested-for-loops/

Now it looks like this:

int[][,] allAtomPositions = new int[][,] {
new int[,] { { 0, 0 } },
new int[,] { { -10, 0 }, { 10, 0 } },
new int[,] { { -10, -7 }, { 12, -7 }, { 0, 7 } } };

It is not exactly the same and it needed minor changes of the following
code because of the different array index syntax for the inner arrays,
but it looks much nicer.

BTW: Why did the webpage need to write "new int[3][,]", but I can write
"new int[][,]"? I'm using Unity, I guess with Mono, don't know which C#
version.
 
Ad

Advertisements

A

Arne Vajhøj

Am 24.12.2014 um 01:22 schrieb Arne Vajhøj:
Jagged arrays are more tricky. You can omit the new in the outermost and
you can omit the type in the innermost. The rest is needed.

int[][][] pos = { new int[][] { new [] { 0, 0 } },
new int[][] { new [] { -10, 0 }, new [] {
10, 0 } },
new int[][] { new [] { -10, -7 }, new [] {
12, -7 }, new [] { 0, 7 } } };
Still ugly. I found a trick on this webpage, mixing jagged and
multidimensional arrays:

http://www.csharphelp.com/2007/09/c-simple-rectangular-mixed-and-jagged-arrays-foreach-loops-and-nested-for-loops/

Now it looks like this:

int[][,] allAtomPositions = new int[][,] {
new int[,] { { 0, 0 } },
new int[,] { { -10, 0 }, { 10, 0 } },
new int[,] { { -10, -7 }, { 12, -7 }, { 0, 7 } } };

It is not exactly the same and it needed minor changes of the following
code because of the different array index syntax for the inner arrays,
but it looks much nicer.

BTW: Why did the webpage need to write "new int[3][,]", but I can write
"new int[][,]"? I'm using Unity, I guess with Mono, don't know which C#
version.
I think that feature has existed since C# 1.0.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to initialize jagged array? 2
jagged arrays 0
Jagged arrays 5
Jagged arrays question 3
Indexer to Jagged array 8
Serializing a Jagged Array 0
Jagged array problem 3
Jagged arrays and datagridviews 0

Top