-3

I have a 2-d int array say

int[,] data = new int[N, numOfCol]; 

Now this is also numOfCol number of 1-d array of length N. Correct?

Suppose numOfCol is 3.

How do we represent those 1-d arrays?

data[,0] data[,1] and data[,2] gives me wrong syntax in Visual Studio.

Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
blue piranha
  • 3,706
  • 13
  • 57
  • 98

3 Answers3

1

Your question is a bit confusing, because your title is about splitting an array, while the body of your question is about the syntax of declaring an array.

If you'd like to split a 2D array into a jagged array (i.e. an array of arrays), use something like this:

int[,] input = ...

int[][] output = Enumerable.Range(0, input.GetLength(0))
    .Select(x => Enumerable.Range(0, input.GetLength(1))
        .Select(y => input[x, y])
        .ToArray())
    .ToArray();

If all you'd like to do is declare a jagged array, see Garath's answer.

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

You have to change your data structure to two dimension array but in format

int[][] data = new int[max number of columns][]

You could see very good explanation in this post Multidimensional Array [][] vs [,]

Community
  • 1
  • 1
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
0

try this..

    for(int i =0;i<N;i++){
     //in each row

        for(int j=0;j<numOfCol;j++){
         //in each column

          var value = data[i,j]; //like data[0,0] data[0,1] data[0,2].... 
          //you have go through each row and column or directly if
          // you have data in 2nd row and 3rd column, you can write data[1,2]
        }
    }
Jayant Varshney
  • 1,765
  • 1
  • 25
  • 42