I'm working with 3D coordinates. I have them all saved into a List, but to keep working with them, I need to have them into a multidimensional array (float[,]).
My list looks like this:
<Coordinates> hp_List = new List<Coordinates>();
public class Coordinates
{
public float x { get; set; }
public float y { get; set; }
public float z { get; set; }
}
I tried the following code:
int R = hp_List.Count();
float[,] hp_array = new float[R, 3];
for(int i=0; i<R; i++)
{
for (int j = 0; j < hp_List.Count; j++)
{
hp_array[i, 0] = hp_List[j].x;
hp_array[i, 1] = hp_List[j].y;
hp_array[i, 2] = hp_List[j].z;
}
}
and I also tried this other way:
for(int i=0; i<R; i++)
{
foreach (Coordinates hp_position in hp_List)
{
hp_array[i, 0] = hp_position.x;
hp_array[i, 1] = hp_position.y;
hp_array[i, 2] = hp_position.z;
}
}
I expected the following output:
589,5 -75,4 238,4
46,2 173,2 70,9
45,7 173,4 70,9
160,9 75,5 75,4
160 76 75,2
156,1 83,9 73,6
My actual output is
156,1
83,9
73,6
156,1
83,9
73,6
156,1
83,9
73,6
156,1
83,9
73,6
156,1
83,9
73,6
156,1
83,9
73,6
which as you can see is the last element of my list.
I'm not sure where my mistake is.
Both loops are iterating through all the elements in the list
Don't forget that
R == hp_List.Count. That is why all the rows in the array contain the last three elements of the list.Try discarding the inner loop.