如何在 C# 中为此创建多维列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create mutlidimensional list for this in C#?
提问by Skuta
I got a table (in file) which I split into blocks by spaces.
我有一个表(在文件中),我按空格将其分成块。
I need structure like this:
我需要这样的结构:
-----------------------------
|21|22|23|33|3323|
|32|32|
|434433|545454|5454|
------------------------------
It's more like each row is its own table. How should I do this?
更像是每一行都是它自己的表。我该怎么做?
I tried List<List<string>> matrix = new List<List<string>>()
; but I can't seem to find a way to work with it.
我试过了List<List<string>> matrix = new List<List<string>>()
;但我似乎找不到处理它的方法。
EDIT- can someone tell me what's wrong with this code???? Matrix[0][0] is same as matrix [1][0].. it seems that same row is added to matrix all the time, but I clear it ...
编辑- 有人能告诉我这段代码有什么问题吗????Matrix[0][0] 与矩阵 [1][0] .. 似乎同一行一直添加到矩阵中,但我清除了它......
static ArrayList ReadFromFile(string filename)
StreamReader SR;
string S;
string[] S_split;
SR = File.OpenText(filename);
S = SR.ReadLine();
ArrayList myItems = new ArrayList();
List<List<string>> matrix = new List<List<string>>();
List<string> row = new List<string>();
while (S != null)
{
row.Clear();
S_split = S.Split(' ');
for (int i = 1; i < S_split.GetLength(0); i++)
{
row.Add(S_split[i]);
matrix.Add(row);
}
S = SR.ReadLine();
}
Console.WriteLine(matrix[1][1]);
SR.Close();
return myItems;
}
采纳答案by Gant
Not sure if I understand this correctly.
不确定我是否正确理解这一点。
List<List<int>> table = new List<List<int>>();
List<int> row = new List<int>();
row.Add(21);
row.Add(22);
row.Add(23);
row.Add(33); // and so on
table.Add(row);
row = new List<int>();
row.Add(1001);
row.Add(1002);
table.Add(row);
MessageBox.Show(table[0][3].ToString());
The program should show a message box with text "33".
程序应该显示一个带有文本“33”的消息框。
回答by RSlaughter
You should be able to work with it as you'd expect to deal with a list within a list.
您应该能够像处理列表中的列表一样使用它。
matrix.Add(new List<string>);
matrix[0].Add("a string");
回答by Daniel Brückner
List<List<String>> matrix = new List<List<String>>();
foreach (String line in file)
{
String[] values = line.Split(new Char[] { ' ' });
matrix.Add(new List<String>(values));
}
Just iterate through your file and for every line do the following.
只需遍历您的文件,并对每一行执行以下操作。
- Generate a new List
- Fill it with the data for the line
- Add the list for the current line to your list for the complete file
- 生成新列表
- 用该行的数据填充它
- 将当前行的列表添加到完整文件的列表中
Note that foreach (String line in file)
is just pseudo code. Further you can merge the to lines in the body to a single line.
请注意,这foreach (String line in file)
只是伪代码。此外,您可以将正文中的 to 行合并为一行。
matrix.Add(new List<String>(line.Split(new Char[] { ' ' })));
回答by Davy Landman
You're describing a jagged array. I'm not exactly sure if a List won't be overkill? If you just have to import the data from the file and than use it, a jagged array should be simple enough.
您正在描述一个锯齿状数组。我不确定 List 是否会矫枉过正?如果您只需要从文件中导入数据而不是使用它,那么锯齿状数组应该足够简单了。
Example:
例子:
int[][] jaggedArray = new int[][]
{
new int[] {21, 22, 23, 33, 3323},
new int[] {32, 32},
new int[] {434433, 545454, 5454}
};
you can also buildup the jagged array in a loop while processing your file, like this:
您还可以在处理文件时在循环中构建锯齿状数组,如下所示:
int[][] result = new int[numberOfLines][];
for (int currentLine = 0; currentLine < numberOfLines; currentLine++)
{
String line = fileStream.ReadLine();
int[] values = SplitAndConvertLine(line);
result[currentLine] = values;
}
回答by Autodidact
If you are trying to read from the file then try something like the following (Note the following code has not been run through a compiler)
如果您尝试从文件中读取,请尝试以下操作(注意以下代码尚未通过编译器运行)
List<string[]> matrix = new List<string[]>();
while(!instream.EndOfStream)
{
var values = instream.ReadLine().Split(new[] {' '});
matrix.Add(values);
}