Create a specific multidimensional array

I have a few basic question regarding multidimensional arrays in C #.

I currently have the following array:

float[] matrix = new float[16];

I need to create instead a 2D array with each row containing the previously mentioned 16 floating point numbers. In addition, the number of rows in a 2D array is unknown at the beginning of the program (i.e., it will be based on a variable).

How to create such an array using an efficient data structure?

+4
source share
3 answers

You can do something like this:

const Int32 arraySize = 16;
var list = new List<float[]>();

This gives an empty list containing null elements (arrays) to run. Since you need to add new arrays, you should write this:

var array = new float[arraySize];
// do stuff to the array

And then add it to the list:

list.Add(array);
+1

16 , 4x4 ( 4x4). .

// init the array
float[,] matrix = new float[4,4];

// loop through the array
for(int col = 0; col < matrix.GetLength(0); col++)
  for(int row = 0; row < matrix.GetLength(1); row++)
     Console.WriteLine(matrix[col, row]); 
+1

float[,] arr2D = new float[12,12];

Alternatively you can use a loop

float[][] floats = new float[12][];
for(int i=0; i< 12; i++)
{
floats[i] = new float[12];
}
+1
source

Source: https://habr.com/ru/post/1529208/


All Articles