简体   繁体   English

1维数组到2维数组

[英]1 dimension array to 2 dimension array

I am trying to create an application that reads a textfile with is formated like this : 我正在尝试创建一个读取文本文件的应用程序,其格式如下:

Outremont,3456,4370,15342,546
St-Leonard,6098,12765,10876,1024  
Chapleau,6087,6087,6087,6087

It basically contains on each line a town name and 4 values that represent number of votes for a political party. 它的每一行基本上都包含一个城镇名称和4个值,这些值代表一个政党的票数。

What i need to do is to retrieve each value from each line and then add them together. 我需要做的是从每一行中检索每个值,然后将它们加在一起。

Example: I would retrieve the 2nd value from the first line(3457) and the 2nd value from the second line (6098) and add them together. 示例:我将从第一行(3457)中检索第二个值,并从第二行(6098)中检索第二个值,并将它们加在一起。

So far I know how to split one line into an array so I can get. 到目前为止,我知道如何将一行分割成一个数组,这样我就可以得到。 I am using a streamreader to retrieve the information of the textfile and then I am splitting one line using the Split method. 我使用streamreader读取器检索文本文件的信息,然后使用Split方法拆分一行。

So far my code looks like this 到目前为止,我的代码看起来像这样

public partial class Form1 : Form
    {

        string[] mots;

private void btnLire_Click(object sender, EventArgs e)
    {

        StreamReader reader = new StreamReader(txtCheminAccess.Text);
        while (!reader.EndOfStream)
        {
            string sVal = reader.ReadLine();
            if (sVal.Length > 0)
            {
                mots = sVal.Split(',');

            }
        }
    }

Of course I can't retrieve every line but just one at a time. 当然,我无法检索每一行,但一次只能检索到一行。 In this case, if you refer yourself to the textfile 在这种情况下,如果您引用文本文件

Outremont,3456,4370,15342,546
St-Leonard,6098,12765,10876,1024
Chapleau,6087,6087,6087,6087 

My array called mots will only contains the last line. 我的名为mots数组将仅包含最后一行。 Chapleau,6087,6087,6087,6087

How can I put this 1 dimensional array into a 2 dimensional array so it looks like Array[textfile Lines,mots] ? 如何将这个1维数组放入2维数组中,使其看起来像Array[textfile Lines,mots]

If all you want is the totals, then there's no need to create a two dimensional array. 如果您想要的只是总数,则无需创建二维数组。 Create one array of totals, and then add each item as you read it. 创建一个总计数组,然后在阅读时添加每个项目。 Like this: 像这样:

int totals[] = new totals[4];
foreach (var sVal in File.ReadLines(txtCheminAccess.Text))
{
    if (sVal.Length > 0)
    {
        mots = sVal.Split(',');
        for (int i = 1; i < 5 && i < sVal.Length; ++i)
        {
            int val;
            if (int.TryParse(sVal[i], out val))
            {
                totals[i-1] += val;
            }
        }
    }
}

File.ReadLines reads the file line-by-line. File.ReadLines逐行读取文件。 It's very convenient shorthand for the StreamReader loop that you created. 对于您创建的StreamReader循环,这非常方便。

After reading the line, it's split the way you split it, and then the code goes through the split array to parse the integers and add them to the totals. 阅读该行后,将按照拆分方式进行拆分,然后代码通过split数组解析整数并将其添加到总计中。

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

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