简体   繁体   中英

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.

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.

How do I do this?

My sample file would look like

123|gf|hf|gr|9

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

123|grf|fe|yr|9

So all 123 would be in a list of string array type of length 4 like :

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.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

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. This will take care of validation you need.

Then you can have a List<ArrayofFour> NTE = new List<ArrayofFour>();

I think this is what you need or at least help you get there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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