Zombia
Zombia

Reputation: 91

conver a list of data into 2d array or List in c#

I have a 1D array that got i*50 elements..i is a random number and 50 is fixed..The 1D array looks like "float array[i*50]"

I wanna convert the 1D array into a 2D array like ,"float array[,50]"..how to do it ?

Upvotes: 1

Views: 324

Answers (2)

shenhengbin
shenhengbin

Reputation: 4294

Does like this ?

int k = 3;
float [] a = new float [k*n];
float [,] b = new float [k, n];

for (int i = 0; i < a.length; i++)
    b[i / n, i % n] = a[i];

Upvotes: 3

paulsm4
paulsm4

Reputation: 121789

  1. Allocate your new 2D array
  2. Allocate a new 1D array for each row in the 2D array
  3. Copy the old elements in your new array with a nested loop: column-by-column, row-by-row

Upvotes: 0

Related Questions