C#将数据与其他数据相关联

时间:2015-03-04 08:19:36

标签: c#

我想在同一个字符串[]中关联不同的字符串[]。

我的不同字符串:

  • string [] 名称
  • string [] axes
  • string [] 数据

数据的每3个数据对应的3个数据,的每组3个数据对应于<的唯一数据名称即可。它在顺序中。

我想收集来自不同字符串的数据。

例如:

  • 名称的第一个数据是:Suj01:RIAS
  • 的前3个数据是:X,Y和Z
  • 数据的前3个数据是(在第一行): - 242.807816,1106.551270和1097.119385

(其余的相同架构)

我想收集一下:

Suj01:RIAS
X : -242.807816
Y : 1106.551270
Z : 1097.119385
etc ...

字符串名称只有1行,包含63个项目

字符串只有1行(63 * 3)项(每次X,Y和Z重复3次)

字符串数据有几行,每行有(63 * 3)个项目(每行:有63个3个项目组,每个项目对应一个来自轴的项目,以及每组对应一个名称)

我该怎么做?

这是我当前的程序,它允许我按照自己的意愿创建不同的字符串:

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using System;

public class Recup_donnees_6 : MonoBehaviour {


    void Awake ()
    {
        Application.targetFrameRate = 25;
    }


         void Start()
        {


            StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt");

            using (reader) { 

                string line = " ";
                int lineNumber = 10;

                for (int i = 0; i < lineNumber; i++) { 


                    line = reader.ReadLine();
                    if (line == null) return;
                }

                string line_10;
                line_10 = line;
                string[] names = line_10.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries);

                line = reader.ReadLine(); 

                string line_11;
                line_11 = line;
                string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries); 

                int counter = 0;
                while (line != null) { 
                    counter++;
                    line = reader.ReadLine();
                    if ((counter %3) != 1) 
                    continue;

                    string lines_datas;
                    lines_datas = line;
                    string[] datas;
                    datas = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); // Pb : we can't use "Split" when it's a string []

                    line = reader.ReadLine();

                }
            }

    }
}

此代码运行,不是问题。 我只是想告诉你我现在的代码。

请问你能帮帮我吗?

谢谢

P-s:我不是英国人,所以我尽力写作并尽可能地表达意思。

我在下面这样做:

        class Axe

        {
            private double x = .0;
            private double y = .0;
            private double z = .0;

            public Axe(double _x, double _y, double _z)

            {
                x = _x;
                y = _y;
                z = _z;
            }

            public double X
            {
                get{return x;}
                set{x = value;}
            }

            public double Y
            {
                get{return y;}
                set{y = value;}
            }

            public double Z
            {
                get{return z;}
                set{z = value;}
            }

        }

        class MyData
        {
            private string aName = string.Empty;
            private List<Axe> axes = new List<Axe>();

            public MyData(string _name, List<Axe> _axes)
            {
                aName = _name;
                axes = _axes;
            }

            public string Name
            {
                get{return aName;}
                set {aName = value;}
            }

            public List<Axe> Axes
            {
                get{return axes;}
                set{axes = value;}
            }

        }

    List<MyAData> datas = new List<MyData>();

    for(int i = 0; i < names.Length; i++)

    {
        var myClassVar = new MyData();
        myClassVar.name = names[i];
        myClassVar.axisX = axes[(i*3)];
        myClassVar.axisY = axes[(i*3)+1];
        myClassVar.axisZ = axes[(i*3)+2];
        myClassVar.dataA = datas[(i*3)];
        myClassVar.dataB = datas[(i*3)+1];
        myClassVar.dataC = datas[(i*3)+2];
        listOfAwesome.Add(myClassVar);

    }   

1 个答案:

答案 0 :(得分:0)

而不是维护3个数组,只需创建自己的对象,该对象具有一些有意义的参数名称,然后您可以将项目附加到..以下应该可以工作,但您可能需要进行一些额外的验证以确保axesdatas长度是正确的大小..

List<MyAwesomeClass> listOfAwesome = new List<MyAwesomeClass>();
for(int i = 0; i < names.Length; i++)
{
    var myClassVar = new MyAwesomeClass();
    myClassVar.name = names[i];
    myClassVar.axisX = axes[(i*3)];
    myClassVar.axisY = axes[(i*3)+1];
    myClassVar.axisZ = axes[(i*3)+2];
    myClassVar.dataA = datas[(i*3)];
    myClassVar.dataB = datas[(i*3)+1];
    myClassVar.dataC = datas[(i*3)+2];
    listOfAwesome.Add(myClassVar);
}           

完整代码:

 void Start()
{
    using (StreamReader reader = new StreamReader ("Suj01_PI_DP_C00_1.txt"))
    { 
        string line = " ";
        int lineNumber = 10;

        for (int i = 0; i < lineNumber; i++) 
        { 
            line = reader.ReadLine();
            if (line == null) return;
        }

        string[] names = line.Split (new String[] {",",",,,"},StringSplitOptions.RemoveEmptyEntries);

        string line_11 = reader.ReadLine();;
        string[] axes = line_11.Split(new String[] {"Field #",","},StringSplitOptions.RemoveEmptyEntries); 

        int counter = 0;
        while (line != null) { 
            counter++;
            line = reader.ReadLine();
            if ((counter %3) != 1) 
                continue;

            string lines_datas;
            lines_datas = line;
            string[] datas;
            datas = lines_datas.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); // Pb : we can't use "Split" when it's a string []

            line = reader.ReadLine();
        }

        List<MyAwesomeClass> listOfAwesome = new List<MyAwesomeClass>();
        for(int i = 0; i < names.Length; i++)
        {
            var myClassVar = new MyAwesomeClass();
            myClassVar.name = names[i];
            myClassVar.axisX = axes[(i*3)];
            myClassVar.axisY = axes[(i*3)+1];
            myClassVar.axisZ = axes[(i*3)+2];
            myClassVar.dataA = datas[(i*3)];
            myClassVar.dataB = datas[(i*3)+1];
            myClassVar.dataC = datas[(i*3)+2];
            listOfAwesome.Add(myClassVar);
        }           
    }

编辑:

MyAwesomeClass的一个例子是

public class MyAwesomeClass
{
    public string Name {get;set;}
    public string AxisX {get;set;}
    public string AxisY {get;set;}
    public string AxisZ {get;set;}
    public string DataA {get;set;}
    public string DataB {get;set;}
    public string DataC {get;set;}

    public MyAwesomeClass()
    {
    }
}

但是你应该对此进行扩展,以相应地改变每个值的类型。您可以找到有关课程here

的更多信息