简体   繁体   English

从文件中读取2D矩阵到C#中的2D int数组

[英]Read 2D matrix from file to 2D int array in C#

I have problem in reading 2D text from file and import it to an int array. 我从文件中读取2D文本并将其导入到int数组时遇到问题。 Specifically, my text file looks like below: 具体来说,我的文本文件如下所示:

2,3,4,5,6 2,3,4,5,6

5,2,3,4,5 5,2,3,4,5

2,4,6,7,4 2,4,6,7,4

2,7,8,5,6 2,7,8,5,6

So each cell in matrix is separated by comma and each new row starts with new line. 因此,矩阵中的每个单元格均以逗号分隔,每个新行均以新行开头。

I tried many ways to make it works but I can't! 我尝试了多种方法使其有效,但我做不到! Simply, I want an int[][] or int[,] array at the end. 简而言之,我希望最后使用一个int[][]int[,]数组。

PS: I can read 1-D matrix simply to int[] as below: PS:我可以简单地将一维矩阵读取为int[] ,如下所示:

int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();
// Read the text file
var text = File.ReadAllLines(@"path\to\file.txt");

// Split on `,`, convert to int32, add to array, add to outer array
var result = text.Select(x => (x.Split(',').Select(Int32.Parse).ToArray())).ToArray();

Result is int[][] . 结果是int[][]

Try this: 尝试这个:

String input = File.ReadAllText( @"c:\myfile.txt" );

int i = 0, j = 0;
int[,] res = new int[10, 10];
foreach (var row in input.Split('\n'))
{
   j = 0;
   foreach (var col in row.Trim().Split(' '))
   {
       res[i, j] = int.Parse(col.Trim());
       j++;
   }
   i++;
}

If that dint work you also have an alternative: 如果这种努力工作,您还可以选择:

int[][] list = File.ReadAllLines("myfile.txt")
               .Select(l => l.Split(',').Select(i => int.Parse(i)).ToArray())
               .ToArray();

You first need to add the index of each line to the first dimension and then every 'column 'in the line to the second dimension. 您首先需要将每行的索引添加到第一维,然后将行中的每个“列”添加到第二维。

Try the following code: 尝试以下代码:

int[][] array = File.ReadAllText(filepath).Split('\n')
                .Select(r => (r.Split(','))
                .Select(c => int.Parse(c)).ToArray()).ToArray();

You are reading all lines, but the code then only processes one line. 您正在读取所有行,但是代码仅处理一行。 If you're trying to make a 2D array of integers, you want something like this: 如果您要制作2D整数数组,则需要这样的内容:

string text = @"2,3,4,5,6 5,2,3,4,5 2,4,6,7,4"; 字符串文本= @“ 2,3,4,5,6 5,2,3,4,5 2,4,6,7,4”;

text.Split('\\n').Select(line => line.Split(',').Select(t => int.Parse(t)).ToArray()).ToArray(); text.Split('\\ n')。Select(line => line.Split(',')。Select(t => int.Parse(t))。ToArray())。ToArray();

(where for test purposes I've replaced the file read with a static string) (出于测试目的,我已用静态字符串替换了读取的文件)

Just to illustrate the point, the problem, that you have is storing the references of each line into a jagged array int[][] or into a bi-dimensional array int[,] 只是为了说明问题,您正在将每行的引用存储到锯齿状数组int [] []或二维数组int [,]中

I suggest you to take a look here before https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396 我建议您在https://msdn.microsoft.com/zh-cn/library/aa288453%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396之前先看一下这里

If you want to create a jagged array is simple, the first dimension will be the amount of lines that you have on your file. 如果要创建一个锯齿状数组很简单,则第一维将是文件上的行数。 Remember this jagged array has arrays in each positions 记住这个锯齿状的数组在每个位置都有数组

int[][] a = new int[amountOfLinesTxt][];

because you can get the array per line, the only thing that you need to do is to assign the array to the specific position for example 因为您可以按行获取数组,所以唯一要做的就是将数组分配给特定位置,例如

int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();
a[0] = array

Same thing for the other lines. 其他行也一样。

Now if you want to use a bi-dimensional array you need to specify in your case from the begging the dimensions, for example int[,] a = new int[amountOfRowsTxt, amountofColumnsTxt]; 现在,如果您要使用二维数组,则需要从乞讨的维度开始进行指定,例如int[,] a = new int[amountOfRowsTxt, amountofColumnsTxt];

then while you are reading your lines you need to save the items inside of it. 那么当您阅读行时,您需要将项目保存在其中。

int[] array= File.ReadAllText(fileppath).Split(',').Select(t => int.Parse(t)).ToArray();

for(int i = 0; array.Length; i ++)
{
   a[currentRow, i] = array[i];
}

Got the idea? 有这个主意吗? Of course there are better ways to get the result using Linq, just take a look on the previous answers. 当然,只要看一下先前的答案,就有更好的方法使用Linq获得结果。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM