Reputation: 37
First timer here. StackOverflow has helped me to somewhat grok arrays and lists, but I'm running into a problem that I don't see answered elsewhere.
Goal: take an existing multidimensional array, read each line and if it starts with the correct identifier, pull that line into a list. Each list becomes part of my list of lists.
The problem: if I use newGrid[0].Add(CSVReader.grid[x,y]); below it runs fine. But if I use [y] instead of [0] I get an exception. Doesn't y = 0 at the point I'm getting the exception? I would like to use the for loop's y to make each line pulled from the array a new list.
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);
}
}
}
}
Upvotes: 0
Views: 334
Reputation: 100545
Line numbers in your newGrid
don't match line numbers in CSVReader.grid
because you only copy some lines from source array.
Instead of reusing y
use separate variable that counts lines in the newGrid
each time you call newGrid.Add
or simply add elements to last row.
Upvotes: 2