简体   繁体   English

C#需要帮助将文本文件读取为2D或锯齿状数组

[英]C# Need help to read text file to 2D or jagged array

I'am working with SMACOF algorythm and have a little problem. 我正在使用SMACOF算法,但有一些问题。 I'am trying to read text file wich contains elements like this: 我正在尝试读取包含以下内容的文本文件:

5.1 3.5 1.4 0.2 
4.9 3.0 1.4 0.2 
4.7 3.2 1.3 0.2 
4.6 3.1 1.5 0.2

Where are few hundred lines of these elements. 这些元素几百行。 So far I have managed to read just first line and don't know why it's not reading all lines. 到目前为止,我已经设法仅读取第一行,并且不知道为什么它不读取所有行。 Here is my code: 这是我的代码:

    static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines("duom.txt");
        string[][] grid = new string[lines.Length][];
        for (int i = 0; i < lines.Length; i++)
        {
            grid[i] = lines[i].Split(',');
        }
        for (int i = 0; i < lines.Length; i++)
        {
            for (int j = 0; j < lines[i].Length; j++)
            {
                Console.WriteLine(grid[i][j]);
                Console.ReadLine();
            }
        }
    }

So, maybe you guys can explain what is wrong with my code, and how propely read text file? 因此,也许你们可以解释我的代码有什么问题,以及如何正确读取文本文件? Thanks. 谢谢。

Your second loop will go out of bounds as your looping over the length of the line (in characters) not the length of the array in grid[i]. 您的第二个循环将超出范围,因为您将循环遍历行的长度(以字符为单位)而不是grid [i]中数组的长度。

    static void Main(string[] args)
    {
        string[] lines = File.ReadAllLines("duom.txt");
        string[][] grid = new string[lines.Length][];
        for (int i = 0; i < lines.Length; i++)
        {
            grid[i] = lines[i].Split(',');
        }
        for (int i = 0; i < lines.Length; i++)
        {
            for (int j = 0; j < **grid**[i].Length; j++)
            {
                Console.WriteLine(grid[i][j]);
                Console.ReadLine();
            }
        }
    }

Wouldn't the code below suffice, its shorter and prevents copying the whole file into memory before transforming to an array. 下面的代码不够用,因为代码较短,并且无法在转换为数组之前将整个文件复制到内存中。

var grid  = File.ReadLines("duom.txt")
    .Select(line => line.Split(' ').Select(item => double.Parse).ToArray())
    .ToArray();

foreach(var item in grid.SelectMany(row => row))
{
    Console.WriteLine(item);
}

Maybe I'm missing something but this Split(',') looks shady. 也许我错过了一些东西,但是这个Split(',')看起来很阴暗。 I dont see ',' in your example. 我在您的示例中看不到',' Try Split(' ') instead! 尝试使用Split(' ')

Thanks everyone for help, but I managed to fix by myself, here is solution: 感谢大家的帮助,但是我设法自己解决了,这是解决方法:

int h = 0;
        int g = 0;
        string linijos = File.ReadAllText("duom.txt");
        double[][] nuskait = new double[150][];
        foreach (var row in linijos.Split('\n'))
        {
            h = 0;
            nuskait[g] = new double[4];
            foreach (var col in row.Trim().Split(' '))
            {
                nuskait[g][h] = double.Parse(col);
                h++;
            }
        }
        g++;

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

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