繁体   English   中英

如何声明和使用字符串数组列表?

[英]How to declare and use a list of string array?

我需要在文件中读取一行。 根据文件中的前3个字符,我可以确定一种记录类型。

这表示该行需要拆分的字符串数。

我需要在List中保存相同类型的所有行。

我该怎么做呢?

我的示例文件看起来像

123 | GF | HF | GR | 9

145 * GF * 43 * 434 * 9 * 645 * 554

123 | GRF | FE |年| 9

因此所有123都将在长度为4的字符串数组类型列表中,如:

public List<string[]> NTE =new List<string[4]>();

除了声明长度未被编译器接受

你可以用

List<string[]> NTE =new List<string[]>();

然后,当您需要向NTE添加元素时,您只需要指定大小为4

NTE.Add(new string[4]); //here it is defined having size of 4, not in the list declaration

然后当你使用它:

NTE[0] = ...something

那将是一个string[4]数组

class ArrayofFour
{
    string[] a = new string[4];

    public string this[int i]
    {
        get
        {
            return a[i];
        }
        set
        {
            a[i] = value;
        }
    }
}

使用ArrayofFour而不是数组,您可以像使用索引器的数组一样使用它。 这将负责您需要的验证。

然后你可以有一个List<ArrayofFour> NTE = new List<ArrayofFour>();

我认为这是你需要的,或者至少可以帮助你实现目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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