-2

I have a 2d array group

float[,] group = new float [2,3];

My understanding is that, after this line of code I have 2 3-elements arrays in group.

{{3elements},{3elements}}

I also have member

float[] member = new float[3] {1,2,3};

I want to set the first array inside the 2d array "group" to match "member" so that I can have {{1,2,3},{}}

I tried group[0] = member;

But I got an error. What's the correct way of doing this?

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • _"I have 2 3-elements arrays in group"_ -- your understanding is incorrect. You have a **two-dimensional** array. It's a single array, with rows and columns. If you want for an array of array, you need a **jagged array**, e.g. `float[][] group = { new float[3], new float[3] };`. Of course, if you're just going to initialize the members explictly, then you can just do `float[][] group = new float[][2]; group[0] = member;` It's not clear what it is you _actually_ want here. – Peter Duniho Oct 16 '20 at 04:14
  • @SvenRasmussen: no, that's incorrect. If they specify both indexes for the 2d array, then they can only assign a single `float` value, not a `float[]` like they want. – Peter Duniho Oct 16 '20 at 04:16
  • See also https://stackoverflow.com/questions/16339883/is-it-possible-to-copy-a-1d-array-to-a-2d-array-as-described-and-if-so-how, https://stackoverflow.com/questions/12567329/multidimensional-array-vs, and https://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays – Peter Duniho Oct 16 '20 at 04:26
  • Thanks guys. I thought doing float[2,3] group and float[][] group = { new float[3], new float[3] } is just that the second method allows arrays of different lengths to be inside the parent array. What are the differences between these two methods beside "doing different child array length" and "set members explicitly"? – user1555707 Oct 16 '20 at 04:33
  • Good read, thanks Peter – user1555707 Oct 16 '20 at 04:35

1 Answers1

0

You can try this

        float[,] group = new float[2, 3];
        float[] member = new float[3] { 1, 2, 3 };

        for(int i = 0; i < member.Length; i++)
        {
            group[0, i] = member[i];
        }
Sekhar
  • 5,614
  • 9
  • 38
  • 44