[英]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.