【发布时间】:2013-02-13 06:19:08
【问题描述】:
这里是第一个计时器。 StackOverflow 帮助我在一定程度上了解了数组和列表,但我遇到了一个在其他地方没有得到解答的问题。
目标:获取一个现有的多维数组,读取每一行,如果它以正确的标识符开头,则将该行拉入一个列表。每个列表都成为我列表的一部分。
问题:如果我使用 newGrid[0].Add(CSVReader.grid[x,y]);在它下面运行良好。但是如果我使用 [y] 而不是 [0] 我会得到一个异常。在我遇到异常时,y = 0 不是吗?我想使用 for 循环的 y 使从数组中拉出的每一行都成为一个新列表。
public void processCSV () {
List<List<string>> newGrid = new List<List<string>>();
for (int y = 0; y < CSVReader.grid.GetUpperBound(1); y++) {
if (CSVReader.grid[0,y] == "T1") {
newGrid.Add(new List<string>());
for (int x = 0; x < CSVReader.grid.GetUpperBound(0); x++) {
newGrid[y].Add(CSVReader.grid[x,y]);
}
foreach(string item in newGrid[y]) {
print(item);
}
}
}
}
【问题讨论】:
-
因为 newGrid.Add(new List
());在 IF 语句中,y 不一定与您列表中的项目数相对应。这可能是您的问题吗?