简体   繁体   中英

How to copy a List<T> to a multidimensional array with a for-loop?

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

for(int i=0; i<R; i++)
{
    for (int j = 0; j < hp_List.Count; j++)
    {
    ////
    }
}

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.

for(int i=0; i<R; i++)
{         
    hp_array[i, 0] = hp_List[i].x;
    hp_array[i, 1] = hp_List[i].y;
    hp_array[i, 2] = hp_List[i].z;
}
        List<Coordinates> hp_List = new List<Coordinates>
        {
            new Coordinates
            {
                 x = 1,
                 y = 2,
                 z = 3
            },
            new Coordinates
            {
                 x = 4,
                 y = 5,
                 z = 6
            }
        };

        int R = hp_List.Count();
        float[,,] hp_array = new float[R, 1, 3];
        for (int i = 0; i < R; i++)
        {
            for (int j = 0; j < 1; j++)
            {
                hp_array[i, j, 0] = hp_List[j].x;
                hp_array[i, j, 1] = hp_List[j].y;
                hp_array[i, j, 2] = hp_List[j].z;
            }
        }
        Console.WriteLine(string.Format("X Y Z"));
        for (int i =  0; i< R; i++)
        {
            Console.WriteLine(string.Format("{0} {1} {2}", hp_array[i, 0, 0], hp_array[i, 0, 1], hp_array[i, 0, 2]));
        }
        Console.Read();

Out Put

XYZ

1 2 3

4 5 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM