C#如何创建存储数据的数组/矩阵?

时间:2016-12-04 06:13:51

标签: c# arrays

我的文件如下: 你好,1,2-,向上 再见,0,4,下 ...

我有WordToFind会有我文件中的第一个单词,但我希望它有两个单词' hello'并且'再见'。 同样对于X1Coordinate,我希望它有' 1'和' 0',但作为整数。 那么如何修改我的代码呢? 我是一个初学者,我试图弄清楚它超过3个小时但仍然无法管理,所以如果这是一个非常基本的问题,我很抱歉。

for (int m = 0; m < Words; m++)
{
    string LinesInFile = reader.ReadLine();

    string[] WordsWithSpecifics = LinesInFile.Split(',');
    string WordToFind = WordsWithSpecifics[0];
    int X1Coordinate = int.Parse(WordsWithSpecifics[1]);
    int Y1Coordinate = int.Parse(WordsWithSpecifics[2]);
    string WordDirection = WordsWithSpecifics[3];

1 个答案:

答案 0 :(得分:0)

现在我得到你需要的东西。下面的代码将满足您的要求:

public int Max(int number){ }

但我个人建议使用类来存储结构化数据,如下所示:

var WordToFind = new List<string>();
var X1Coordinate = new List<int>();
var Y1Coordinate = new List<int>();
var WordDirection = new List<string>();
while((LinesInFile = reader.ReadLine()) != null)  
{
    string[] WordsWithSpecifics = LinesInFile.Split(',');
    WordToFind.Add(WordsWithSpecifics[0]);
    X1Coordinate.Add(int.Parse(WordsWithSpecifics[1]));
    Y1Coordinate.Add(int.Parse(WordsWithSpecifics[2]));
    WordDirection.Add(WordsWithSpecifics[3]);
}

然后像这样使用它:

class MyDataType
{
    public string Word { get; set; }
    public int X1 { get; set; }
    public int Y1 { get; set; }
    public string Direction { get; set; }
}