分割2D阵列的最有效方法? [英] Most efficient way to split a 2D array?

查看:51
本文介绍了分割2D阵列的最有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文件读取到全局数组中,遍历该数组并标识每个字符串为其对应的数量,然后在其自己的组中找到总和.然后将这些组分别划分为自己的唯一编号.

I am trying to read a file into a global array, loop through the array and identify each string to it's corresponding amount, and then find the sums in their own groups. and divide those groups each by their own unique number.

我正在阅读的文本文件:

The text file that I am reading in:

Name,50
Name,40
DifferentName,50
AnotherName,10
Name,60

最简单的方法是在逗号之间分割行,并使数字与它们的特定名称相对应,而又不知道它们将按哪个顺序排列?

What would be the easiest way to split the lines at the commas, and make the numbers correspond to their specific names, not knowing which order they will be in?

这是我到目前为止使用的代码.虽然现在只是打开文件对话框,但我将其作为建设性参考.

Here is the code I am using so far..although right now it is just the open file dialog, I am putting this here as a constructive reference.

        string strFileName;

    private void btnReadInFile_Click(object sender, EventArgs e)
    {
        //select the file
        OpenFileDialog ofdGetFile = new OpenFileDialog();
        if (ofdGetFile.ShowDialog() == DialogResult.Cancel)
        {
            //cancel
            MessageBox.Show("User Canceled");
            return;
        }
        else
        {
            //open the file
            StreamReader myStreamReader;
            strFileName = ofdGetFile.FileName;
            FileStream input = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
            myStreamReader = new StreamReader(input);
            // other
            MessageBox.Show("Reading Complete", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private void btnShowGrade_Click(object sender, EventArgs e)
    {
        if (strFileName != null)
        {
            String[][] nameSums = System.IO.File.ReadLines(strFileName)
           .Select(l => l.Split(','))                // split the line
           .GroupBy(arr => arr[0])                   // group by name
           .Select(grp => new String[]{
            grp.Key,
            grp.Sum(arr => int.Parse(arr[1])).ToString()})
           .ToArray();

        }
        else
        {
            MessageBox.Show("You need to read in a file first.");
        }
    }
}

我觉得必须有更好的方法来做到这一点.

I feel like there has to be a better way to do this.

非常感谢您到目前为止的帮助!我敢肯定,这个问题没有得到解决的唯一原因是我缺乏沟通技巧.

Thank you all so much for your help thus far! I am sure that the only reason this problem hasn't been resolved is my lack of communication skills.

推荐答案

myArray = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray();

请注意,这将是一个交错数组,而不是二维数组.要创建一个二维的,您可以使用

Note this will be a staggered array, not a 2 dimensional one. To create a 2 dimensional one, you could use

lines = File.ReadAllLines(myFileName).Select(l => l.Split(',')).ToArray();
myArray = new string[lines.Count(), 2];
for(int i = 0; i < lines.Length; i++)
{
    myArray[i, 0] = lines[i, 0];
    myArray[i, 1] = lines[i, 1];
}

这篇关于分割2D阵列的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆