【问题标题】:Convert members of struct in a list to array将列表中的结构成员转换为数组
【发布时间】:2014-05-10 08:28:11
【问题描述】:

为了处理来自日志文件的数据,我将数据读入一个列表。

当我试图从列表转换为图形例程的数组时,我遇到了麻烦。

为了便于讨论,假设日志文件包含三个值* - x、y 和 theta。在执行文件 I/O 的例程中,我读取了三个值,将它们分配给一个结构并将该结构添加到 PostureList。

绘图例程希望 x、y 和 theta 位于单独的数组中。我的想法是使用 ToArray() 方法进行转换,但是当我尝试下面的语法时,我得到了一个错误 - 请参阅下面的评论中的错误。我有另一种方法来进行转换,但想获得有关更好方法的建议。

我对 C# 很陌生。提前感谢您的帮助。

注意:* 实际上,日志文件包含许多具有不同负载大小的不同信息。

struct PostureStruct
{
    public double x;
    public double y;
    public double theta;
};

List<PostureStruct> PostureList = new List<PostureStruct>();

private void PlotPostureList()
{
    double[] xValue = new double[PostureList.Count()];
    double[] yValue = new double[PostureList.Count()];
    double[] thetaValue = new double[PostureList.Count()];

    // This syntax gives an error:
    //   Error 1 'System.Collections.Generic.List<TestNameSpace.Test.PostureStruct>'
    //   does not contain a definition for 'x' and no extension method 'x' accepting a first
    //   argument of type 'System.Collections.Generic.List<TestNameSpace.Test.PostureStruct>'
    //   could be found (are you missing a using directive or an assembly reference?)
    xValue = PostureList.x.ToArray();
    yValue = PostureList.y.ToArray();
    thetaValue = PostureList.theta.ToArray();

    // I could replace the statements above with something like this but I was wondering if
    // if there was a better way or if I had some basic mistake in the ToArray() syntax.
    for (int i = 0; i < PostureList.Count(); i++)
    {
        xValue[i] = PostureList[i].x;
        yValue[i] = PostureList[i].y;
        thetaValue[i] = PostureList[i].theta;
    }

    return;
}

【问题讨论】:

    标签: c# list struct toarray


    【解决方案1】:

    ToArray 扩展方法只能用于IEnumerables。要将IEnumerable转换,例如从您的结构转换为单个值,您可以使用Select 扩展方法。

    var xValues = PostureList.Select(item => item.x).ToArray();
    var yValues = PostureList.Select(item => item.y).ToArray();
    var thetaValues = PostureList.Select(item => item.theta).ToArray();
    

    您不需要定义数组的大小或使用new 创建它们,扩展方法会处理这些。

    【讨论】:

      【解决方案2】:

      您正试图在列表中直接引用 x。

      PostureList.y
      

      你需要在特定的成员上这样做

      PostureList[0].y
      

      我想您需要从列表中选择所有 x。为此,您可以这样做

      xValue = PostureList.Select(x => x.x).ToArray();
      

      【讨论】:

        【解决方案3】:

        您可以使用这种方式将您的List&lt;PostureStruct&gt; 转换为单独的数组:

        double[] xValue = PostureList.Select(a => a.x).ToArray();
        double[] yValue = PostureList.Select(a => a.y).ToArray();
        double[] thetaValue = PostureList.Select(a => a.theta).ToArray();
        

        这就是您所要做的,数组将具有正确的大小(与列表的长度相同)。

        【讨论】:

          【解决方案4】:

          您可以在列表中循环

            double[] xValue = new double[PostureList.Count()];
            double[] yValue = new double[PostureList.Count()];
            double[] thetaValue = new double[PostureList.Count()];
          
            foreach (int i = 0; i < PostureList.Count; ++i) {
              xValue[i] = PostureList[i].x;
              yValue[i] = PostureList[i].y;
              thetaValue[i] = PostureList[i].theta;
            }
          
            ...
          

          或者使用Linq,但是以不同的方式:

            double[] xValue = PostureList.Select(item => item.x).ToArray();
            double[] yValue = PostureList.Select(item => item.y).ToArray();
            double[] thetaValue = PostureList.Select(item => item.theta).ToArray();
            ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-12-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-05
            相关资源
            最近更新 更多