问题
I am using C# in a console application.
I need to load data from a text file and load it into a 2d array.
This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.
public static int[,] LoadMap()
{
const string path = @"1.txt";
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 15];
string line;
for (int i = 0; i < fileLines.Length; ++i)
{
line = fileLines[i];
for (int j = 0; j < line.Length; ++j)
{
map[i, j] = (int)(line[j] - '0');
}
}
return map;
}
But when I hardcode the data like that, then everything gets displayed perfectly.
private static int[,] Map = new int[MapX, MapY]
{
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
The data in the text file looks like that :
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.
回答1:
You could use LINQ to parse the lines:
var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
for(int j =0; j<25; ++j)
map[i,j] = data[j];
}
return map;
If you could use a jagged array instead of a 2D array, this becomes simpler:
public static int[][] LoadMap()
{
return File.ReadLines(path)
.Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
.ToArray();
}
回答2:
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
for (int i = 0; i < fileLines.Length; ++i)
{
string line = fileLines[i];
for (int j = 0; j < map.GetLength(1); ++j)
{
string[] split = line.Split(',');
map[i, j] = Convert.ToInt32(split[j]);
}
}
return map;
}
回答3:
If your text file has commas separating the values, replace this line:
for (int j = 0; j < line.Length; ++j)
With:
for (int j = 0; j < line.Length; j += 2)
That's assuming your values will always be only 1 char long.
回答4:
When the data comes in from the text file. If it is separated by a comma you can separate it using string.split. Then you load what you get into an array and access it like you normally would do an array. Like below:
string[] lines = System.IO.File.ReadAllLines(@"path");
foreach (string line in lines)
{
string[] first= line.Split(comma);
}
来源:https://stackoverflow.com/questions/19595938/loading-data-into-a-2d-array-from-text-file