Multidimensional Array Hell - C# versus Java => C++ ???

O

Ole

Goodday everybody,

i want to create an array that represents data, that has to
be transferred to a pdf doc ( iTextSharp ).

The problem is, that i seem too loose my faith. Namely, ( i have
experiences in
c/c++, perl and i thouht in c#, too ), i do not succeed in creating a
multidimensional array. I have no experience in creating
multidimensional array in c#. The whole process looks so ugly to me,
that i really
think of throwing all that c# and java stuff against the wind in order
to
use good old c or c++ again.

Or, ... perhaps i'm too stupid.

So, if some programmer out there would like to draw another, deep
fallen programmer out of his depressions, what is wrong with the
following code: (???!!!)



using System;
namespace arraytest {
class Class1 {
[STAThread]
static void Main( string[] args ) {
// shouldn't the following statement create an 3-dimensional array
?
Array numbers = Array.CreateInstance( typeof ( Int32 ), 10, 4, 2
);
// and why isn't possible simply to do :
// numbers[][][] = new int[10][4][2] ?!
try {
for( int i = 0; i < 10; i++ ) {
for ( int y = 0; y < 4; i++ ) {
for( int j = 0; j < 2; j++ ) {
// please do not answer : "why do you not do
numbers.initialize ( )
// wordless!
numbers.SetValue( 100, i,y,j );
}
}
}
}catch ( Exception e ) {
System.Console.WriteLine( e.ToString ( ) );
System.Console.Read( );
}
}
}
}

Sure, i could create a row - class, that would be able to hold the
data and
put those row objects into an array. But i don't want't to
overobjectize
my clients' workstations!!

Please help me out here ...!

Thanks in advance, Ole.

PS ( to all the java programmers : how do you manage array
complications like this ? I saw something similar ugly in java . )

if ( noone.HelpsMe( ) ) {
throw new Languages ( "into the sea" );
}else {

put the System.Out && and goto bed;
}
bed:
 
L

Laxmikant Rashinkar

Well, I dont know a whole lot about C# myself, but think you seem to be
using the wrong
syntax.

For a rectangualr array you would use:
int[,] matrix = new int[10,20] and access it as matrix[3,4] = 5;

For a jagged array you would use:
int[][] jarray = new int[3][]
jarray[0] = new int[10];
jarray[1] = new int[4];
jarray[2] = new int[1024];

To access this, you would use jarray[2][800] = 13;

for what it's worth, my 2 cents!
cheers
LK
 
D

Daniel Billingsley

Well, uh... your code for declaring the array isn't even close to the right
syntax for one thing.

string[,] names = new string[5,4];

works fine for me (straight out of the documentation), as does

string[,,] names = new string[5,4,8];
 
J

Jon Skeet

Ole said:
i want to create an array that represents data, that has to
be transferred to a pdf doc ( iTextSharp ).

Do you really need it to be an array of arrays, or is a rectangular
array okay? If so, just use:

numbers[,,] = new int[10,4,2];

You could cast the result of Array.CreateInstance to int[,,] if you
wanted to.

I don't know why you can't initialise arrays of arrays in the way you
suggested (as you can in Java). Perhaps it's to encourage the use of
rectangular arrays for such cases.
PS ( to all the java programmers : how do you manage array
complications like this ? I saw something similar ugly in java . )

In Java you'd have been fine with:
int[][][] numbers = new int[10][4][2];
 
J

Jon Skeet

Daniel Billingsley said:
Well, uh... your code for declaring the array isn't even close to the right
syntax for one thing.

Yes it is - but only for declaring a jagged array, not a rectangular
array. The only problem is that in C# you can't instantiate a jagged
array in the same way that you can in Java.
 
O

Ole

Jon Skeet said:
Ole said:
i want to create an array that represents data, that has to
be transferred to a pdf doc ( iTextSharp ).

Do you really need it to be an array of arrays, or is a rectangular
array okay? If so, just use:

numbers[,,] = new int[10,4,2];

You could cast the result of Array.CreateInstance to int[,,] if you
wanted to.

I don't know why you can't initialise arrays of arrays in the way you
suggested (as you can in Java). Perhaps it's to encourage the use of
rectangular arrays for such cases.
PS ( to all the java programmers : how do you manage array
complications like this ? I saw something similar ugly in java . )

In Java you'd have been fine with:
int[][][] numbers = new int[10][4][2];

So you say that in java i can say

string [ ][ ] blabla = new string [ 10 ][ 5 ];

and access it like

blabla [ x ] [ y ] = "Glubberlutsch";

Is something similar possible in c# ?

Why do you have to access the other dimensions like as if they
were only other levels of the first dimension !? Or am i completely wrong ?

Greetings Ole.
 
O

Ole

Ok, i (tryto) think i have put my brain on and understood:

int [,,] = new int [10][][];
for rectangular arrays

and

Array bla = Array.CreateInstance ( typeof ( gerede ), new int []{1,2,3} );
for the others ( by the way, how are they called ? irrectangular ? )

Michael Culley said:
Try

int[,,] myArray = new int[10,4,2];

--
Michael Culley


Ole said:
Goodday everybody,

i want to create an array that represents data, that has to
be transferred to a pdf doc ( iTextSharp ).

The problem is, that i seem too loose my faith. Namely, ( i have
experiences in
c/c++, perl and i thouht in c#, too ), i do not succeed in creating a
multidimensional array. I have no experience in creating
multidimensional array in c#. The whole process looks so ugly to me,
that i really
think of throwing all that c# and java stuff against the wind in order
to
use good old c or c++ again.

Or, ... perhaps i'm too stupid.

So, if some programmer out there would like to draw another, deep
fallen programmer out of his depressions, what is wrong with the
following code: (???!!!)



using System;
namespace arraytest {
class Class1 {
[STAThread]
static void Main( string[] args ) {
// shouldn't the following statement create an 3-dimensional array
?
Array numbers = Array.CreateInstance( typeof ( Int32 ), 10, 4, 2
);
// and why isn't possible simply to do :
// numbers[][][] = new int[10][4][2] ?!
try {
for( int i = 0; i < 10; i++ ) {
for ( int y = 0; y < 4; i++ ) {
for( int j = 0; j < 2; j++ ) {
// please do not answer : "why do you not do
numbers.initialize ( )
// wordless!
numbers.SetValue( 100, i,y,j );
}
}
}
}catch ( Exception e ) {
System.Console.WriteLine( e.ToString ( ) );
System.Console.Read( );
}
}
}
}

Sure, i could create a row - class, that would be able to hold the
data and
put those row objects into an array. But i don't want't to
overobjectize
my clients' workstations!!

Please help me out here ...!

Thanks in advance, Ole.

PS ( to all the java programmers : how do you manage array
complications like this ? I saw something similar ugly in java . )

if ( noone.HelpsMe( ) ) {
throw new Languages ( "into the sea" );
}else {

put the System.Out && and goto bed;
}
bed:
 
J

Jon Skeet

Ole said:
In Java you'd have been fine with:
int[][][] numbers = new int[10][4][2];

So you say that in java i can say

string [ ][ ] blabla = new string [ 10 ][ 5 ];

and access it like

blabla [ x ] [ y ] = "Glubberlutsch";
Absolutely.

Is something similar possible in c# ?

Yes - as my message showed:

string [,] blabla = new string [10,5];

blabla [x,y] = "Glubberlutsch";

Note however that that's a rectangular array, as opposed to the Java
version which gives an array of arrays. You can create that as well in
C#, but it takes a little bit more work:

string[][] blabla = new string [10][];
for (int i=0; i < blabla.Length; i++)
blabla=new string[5];

blabla [x][y] = "Glubberlutsch";

I suspect the reason that the C# syntax is longer is to encourage
people to use rectangular arrays in such situations.
Why do you have to access the other dimensions like as if they
were only other levels of the first dimension !? Or am i completely wrong ?

I suggest you look into arrays in a C# tutorial - it's not easy to
cover it in detail in news posts.
 
J

Jon Skeet

Ole said:
Ok, i (tryto) think i have put my brain on and understood:

int [,,] = new int [10][][];
for rectangular arrays

No. You've got a rectangular array on the left hand side and a jagged
array on the right.
Array bla = Array.CreateInstance ( typeof ( gerede ), new int []{1,2,3} );
for the others ( by the way, how are they called ? irrectangular ? )

Jagged - or just an array of arrays.
 

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

Top