Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Filling double[][] manually

Tags:

arrays

c#

I am just trying out a source code. This source code has the following line:

double[][] inputs = sourceMatrix.Submatrix(null, 0, 1).ToArray();

Originally, this "inputs" is filled using a matrix, but I am still too inexperienced to use this matrix. I would first like to test it with some hand-coded values.

Could anybody please tell me how to populate double[][] with some values?

I am not really experienced with C# yet. I guess that [][] means a threedimensional array.

In VB6 I would simply say

Redim inputs(2,2)

and then:

inputs(0,0) = 64
inputs(0,1) = 92
inputs(0,2) = 33
inputs(1,0) = 4
inputs(1,1) = 84
inputs(1,2) = 449

etc...

But I guess it is not that easy in C#. If anybody could help, I would be really glad.

Thank you.

like image 268
tmighty Avatar asked Dec 20 '22 09:12

tmighty


1 Answers

a double[][] is a jagged array - it is an array of arrays. To fill that you would fill the outer array with a set of double[] arrays. However, I expect you want a rectangular array: double[,], for example new double[3,2]. There is a short-hand for initializing such arrays:

double[,] data = new double[2, 3] { { 64, 92, 33 }, { 4, 84, 449 } };
double val = data[1, 2]; // 449.0
like image 86
Marc Gravell Avatar answered Jan 09 '23 08:01

Marc Gravell