简体   繁体   English

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

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

I need to read a line in a file. 我需要在文件中读取一行。 Based on the first 3 characters in the file, I can determine a type of record. 根据文件中的前3个字符,我可以确定一种记录类型。

This indicates the number of strings the line needs to be split into. 这表示该行需要拆分的字符串数。

I need to hold all lines of the same type in a List. 我需要在List中保存相同类型的所有行。

How do I do this? 我该怎么做呢?

My sample file would look like 我的示例文件看起来像

123|gf|hf|gr|9 123 | GF | HF | GR | 9

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

123|grf|fe|yr|9 123 | GRF | FE |年| 9

So all 123 would be in a list of string array type of length 4 like : 因此所有123都将在长度为4的字符串数组类型列表中,如:

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

Except declaring a length isn't being accepted by the compiler 除了声明长度未被编译器接受

You could use 你可以用

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

And then as you need to add an element to the NTE , you only need to specify that the size will be 4 : 然后,当您需要向NTE添加元素时,您只需要指定大小为4

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

Then when you use it: 然后当你使用它:

NTE[0] = ...something

That is going to be a string[4] array 那将是一个string[4]数组

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

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

Use the ArrayofFour instead of an array, you can use it like an array using the indexers. 使用ArrayofFour而不是数组,您可以像使用索引器的数组一样使用它。 This will take care of validation you need. 这将负责您需要的验证。

Then you can have a List<ArrayofFour> NTE = new List<ArrayofFour>(); 然后你可以有一个List<ArrayofFour> NTE = new List<ArrayofFour>();

I think this is what you need or at least help you get there. 我认为这是你需要的,或者至少可以帮助你实现目标。

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

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