Arrays in C# and Java
These are some old facts (c. 2002) but as I had to remember them just yesterday I though I would blog about it...
Following my Java to C# numerical algorithms translation, I hit the matrix package (ok, namespace, project). In there, I see this Java code:
double[][] components = new double[n][m];
This syntax doesn't compile in C#, which takes us to the fact that C# has two kinds of arrays while Java has only one.
Regular arrays in C# are rectangles and use this syntax:
double[,] regular = new double[n,m];
These behave just like matrices in math and their existence is justified because the access to the regular[i,j] elements is very efficient.
Then we have C# jagged arrays:
double[][]jagged = new double[10][];
Random random = new Random();
for (int i = 0; i < jagged.Length; i++)
jagged[i] = new double[random.Next(20)];
Here jagged has 10 rows and each row has a different number of columns, obviously these arrays save memory but accessing jagged[i][j] is not as efficient as with regular[i,j].
On the other side of the fence, Java only has jagged arrays (but I don't know if the compiler/JVM are smart and recognize rectangular arrays so to treat them efficiently), so the Java code:
double[][] comp = comp[n][m];
Is a short hand for the C# code:
double[][] comp = new double[n][];
for (int i = 0; i < comp.Length; i++)
comp[i] = new double[m];
Now, as it was in my case, if what I needed is a rectangular array I would've used :
double[,] comp = new double[n,m];
So, talking about arrays, C# gives you more control but you have to know your options, whereas in Java there's only one way to do it. As they say in Spider Man, with power comes responsibility.