Pro*_*kie -5 c# arrays text 2d file
我有一个看起来像这样的文本文件
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Run Code Online (Sandbox Code Playgroud)
在10 x 10网格中.使用c#我需要获取文本文件并将其转换为2d整数数组,以便我可以在独立级别上操作整数.请帮忙不能解决,
String input = File.ReadAllText( @"c:\myfile.txt" );
int i = 0, j = 0;
int[,] result = new int[10, 10];
foreach (var row in input.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(' '))
{
result[i, j] = int.Parse(col.Trim());
j++;
}
i++;
}
Run Code Online (Sandbox Code Playgroud)
索引将从0开始,因此如果要访问第四行中的第10列:
Console.WriteLine(result[3,9]); //40
Run Code Online (Sandbox Code Playgroud)
一个锯齿状的阵列?
int[][] list = File.ReadAllLines("a.txt")
.Select(l => l.Split(' ').Select(i => int.Parse(i)).ToArray())
.ToArray();
Run Code Online (Sandbox Code Playgroud)
编辑
你可以在这里使用JaggedToMultidimensional
int[,] list2 = JaggedToMultidimensional(list);
Run Code Online (Sandbox Code Playgroud)