Should i use Array, double[][] or double[,]?

K

K Viltersten

I'm designing a class that will base its
functionality on one (or more) arrays of
doubles. Each array will be of the same
length pairwise but their number may
vary from a single one up to several.

The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

In C# i have two more options. Given
that the number of arrays might vary as
the program goes on (as in: i'd like to
remove and add arrays on the fly), i
wonder if there's a gain in deploying
Array class directly.

Also, which would be more prefered in
C# - the jagged or the multidimensional
array?
 
P

Peter Duniho

I'm designing a class that will base its
functionality on one (or more) arrays of
doubles. Each array will be of the same
length pairwise but their number may vary from a single one up to
several.

What does "will be of the same length pairwise" mean? Pairwise to what?
Each other? Something else? Do you just mean that every double[] will be
exactly the same length?
The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

Granted, it's been awhile since I've done a lot of C++ coding. It seems
that every year that goes by, I forget at least one other important part
of the language.

But my recollection is that a declaration like that is quite a bit
different from what you've described so far (the above describes a
two-dimensional array of pointers to doubles, not just to doubles), and
isn't allowed unless you provide the exact dimensions of the array in the
declaration (either in the variable declaration itself or an initializer).
In C# i have two more options. Given that the number of arrays might
vary as
the program goes on (as in: i'd like to
remove and add arrays on the fly), i wonder if there's a gain in
deploying Array class directly.

Also, which would be more prefered in
C# - the jagged or the multidimensional
array?

From your description, possibly neither.

In particular, you wrote: "i'd like to remove and add arrays on the fly".
If by this you mean that you'd add or remove single-dimensional arrays
from the array of arrays, then it seems that using a dynamic collection of
arrays would be better. For example, List<double[]>.

If you simply mean that you'd add and remove these
jagged/multi-dimensional arrays dynamically to and from some other
collection, then it really just comes down to what represents the data the
best. The multi-dimensional array works best for situations when you
really are using all of the elements. A jagged array works best if the
second dimension referenced by the first can vary in size according to the
second. Which shouldn't be surprising, since those statements are not far
from describing precisely what the multi-dimensional array and jagged
array are. :)

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Konrad,

Personally, I would use a List<double[]>. You could treat it as a
multi-dimensional array or a jagged array (since every element in the list
doesn't have to be the same length), and you don't have to worry about
re-copying the array as the number of arrays grows.
 
A

Arne Vajhøj

K said:
The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

I don't think so. It is not valid C++ syntax.

In C# i have two more options. Given that the number of arrays might
vary as
the program goes on (as in: i'd like to
remove and add arrays on the fly), i wonder if there's a gain in
deploying Array class directly.

The Array class is the base class for arrays - it is not a different
type of arrays.
Also, which would be more prefered in
C# - the jagged or the multidimensional
array?

square data => multi dim

non square data => jagged

Arne
 
M

Michael C

Nicholas Paldino said:
Konrad,

Personally, I would use a List<double[]>. You could treat it as a
multi-dimensional array or a jagged array (since every element in the list
doesn't have to be the same length), and you don't have to worry about
re-copying the array as the number of arrays grows.

The other option is List<List<double>>

Michael
 
K

K Viltersten

I'm designing a class that will base its functionality
What does "will be of the same length pairwise" mean?
Pairwise to what? Each other?

Yes. As in - for any pair of the elements you pick, the
length will be equal. I thought it was a common
expression... Too much math, i guess...
The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

But my recollection is that a declaration like that is quite
a bit different from what you've described so far...

It might. The message got through and that's good enough
for me at this point.
In particular, you wrote: "i'd like to remove and add arrays
on the fly". ...[it] seems that using a dynamic collection of
arrays would be better. For example, List<double[]>.

Thanks, that's what i was looking for!
 
K

K Viltersten

I'm designing a class that will base its
functionality on one (or more) arrays of
doubles. Each array will be of the same
length pairwise but their number may vary
from a single one up to several.

Personally, I would use a List<double[]>.
You could treat it as a multi-dimensional
array or a jagged array (since every
element in the list doesn't have to be
the same length), and you don't have to
worry about re-copying the array as the
number of arrays grows.

Yes, that's along the other replies as well
so i see no problem using it. (I'm sure
Peter D. will gladly point out at least one
ust to create a pathological example. The
occasion is on me!)
 
K

K Viltersten

Personally said:
The other option is List<List<double>>

Acutally, in my case, the first way is most
appropriate because the lengths of the
inner arrays will never change, only their
number (if that!).

Thanks anyway.
 
K

K Viltersten

The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

I don't think so. It is not valid C++ syntax.


Allright, already, so i made an ass by
giving "sort-of" code. Big deal! It's
not like a compiler would misunderstand
ones intentions! Oh, wait. It would... :)
 
M

Michael C

K Viltersten said:
Acutally, in my case, the first way is most
appropriate because the lengths of the inner arrays will never change,
only their
number (if that!).

Yep, this method is only useful if the arrays can change in size.
 
A

Arne Vajhøj

K said:
The usual way i'd use in C++ would be to
declare the following.
double[][] *data;

I don't think so. It is not valid C++ syntax.

Allright, already, so i made an ass by
giving "sort-of" code. Big deal! It's not like a compiler would
misunderstand ones intentions! Oh, wait. It would... :)

Compilers are known to be rather picky.

:)

I still remember the Fortran compiler 25 years ago
that would not compile a program because it had an
empty line at the bottom ....

Arne
 
M

Michael C

Arne Vajhøj said:
I still remember the Fortran compiler 25 years ago
that would not compile a program because it had an
empty line at the bottom ....

I still use an 8051 compiler than won't compile unless you have an empty
line.

Michael
 
B

Ben Voigt [C++ MVP]

Michael said:
I still use an 8051 compiler than won't compile unless you have an
empty line.

Assuming you mean C here --

preprocessor directives are required to end in a newline, not end of file

So if the last line is #endif, then yes that's expected.

If the last line was for example }, then I believe that's a nonconformant
compiler
 
M

Michael C

Ben Voigt said:
Assuming you mean C here --

preprocessor directives are required to end in a newline, not end of file

So if the last line is #endif, then yes that's expected.

If the last line was for example }, then I believe that's a nonconformant
compiler

This is a free assembly language compiler. I can't remember the name of it
or who wrote it as it's been a few months since I've used it.

Michael
 

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