从文本文件转换成并行阵列拆分数据 [英] Splitting data from a text file into parallel arrays

查看:110
本文介绍了从文本文件转换成并行阵列拆分数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授给类的C#,可以用于将数据从文本文件分割的例子。我想用它来做,涉及分割一个txt的内容的项目。文件到4阵列或字段。这里是code:

My professor gave the class an example of C# that can be used to split data from a text file. I am trying to use it for a project that involves splitting the contents of a txt. file into 4 arrays or fields. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
    static void Main()
    {
        int i = 0;
        foreach (string line in File.ReadAllLines("census.txt"))
        {
            string[] parts = line.Split(',');
            foreach (string part in parts)
            {
                Console.WriteLine("{0}",

                    part);
            }
            i++; 
        }
    }
}

下面就是 census.txt

21,f, s, 14

41,f, m, 22

12, m, s, 12

11, f, s, 8

29, m, m, 4

6, m, s, 12

9, f, s, 2

30, f, s, 1

这应该是假设的人口普查数据按年龄,性别,婚姻状况,以及区去。我不断收到输出是其中每个数字或字符在像这样一行:

It's supposed to be hypothetical census data going by age, gender, marital status, and district. The output I keep getting is each of those numbers or chars in a single line like so:

21

f

s

14

41

f

m

22

等。

我认为,这意味着它的工作,但我想知道如何使用这个订立4个并行阵列。我也想知道更多关于拆分为4个区域,结构,或类。该项目的下一部分涉及到每一个一定年龄号码或区号码出现,这将涉及大量阵列计时。

I think it means it's working but I'd like to know how to use this for entering into 4 parallel arrays. I would also to know more about splitting it into 4 fields, structs, or classes. The next part of the project involves counting every time a certain age number or district number appears and that will involve a lot of arrays.

推荐答案

一个泛型列表(如这里的其他2个电流答案使用)是最好的一段路要走。但是,如果您需要在阵列中的数据(如您的previous <一个href=\"http://stackoverflow.com/questions/15859398/i-have-no-idea-what-i-am-doingmanaging-multiple-arrays-of-data-and-counting-eac#comment22573331_15859398\">question似乎预示),那么你可以修改你的教授的code是这样的:

A generic list (as used in the other 2 current answers here) is the best way to go. However, if you need to have the data in arrays (as your previous question seems to indicate), then you can modify your professor's code like this:

C#

int[] districtDataD = new int[900];
string[] districtDataG = new string[900];
string[] districtDataM = new string[900];
int[] districtDataA = new int[900];

int i = 0;
foreach (string line in File.ReadAllLines("census.txt"))
{
    string[] parts = line.Split(',');

    districtDataD[i] = int.Parse(parts[0]);
    districtDataS[i] = parts[1];
    districtDataM[i] = parts[2];
    districtDataA[i] = int.Parse(parts[3]);
    i++;
}

VB.NET(因为你原来的问题被标记用VB.NET):

VB.NET (Since your original question was tagged with VB.NET):

Dim districtDataD() As New Integer(900)
Dim districtDataS() As New String(900)
Dim distrcitDataM() As New String(900)
Dim districtDataA() As New Integer(900)

Dim i As Integer = 0

For Each Dim line As String In File.ReadAllLines("census.txt")
    Dim string() As parts = line.Split(',')

    districtDataD(i) = Integer.Parse(parts(0))
    districtDataS(i) = parts(1)
    districtDataM(i) = parts(2)
    districtDataA(i) = Integer.Parse(parts(3))

    i++
Next

您也可以使用结构并有一个数组保存该对象,但它看起来像你的教授要你使用4个独立的阵列。如果你可以使用一个,你可以简单的声明是这样的阵列,例如:

You could also use a struct or class and have one array that holds that object, but it looks like you're professor wants you to use 4 separate arrays. If you can use one, you can simply declare the array like this, for example:

C#

Person[] districtData = new Person[900];

VB.NET

VB.NET

Dim districtData() As New Person(900)

然后,你可以做到这一点,拆分逻辑中(注意,如果说Distric和年龄都在你的对象整数你必须投或解析他们为我展示如下):

Then you could do this inside the split logic (note that if, say Distric and Age are integers in your object you'll have to cast or parse them as I show below):

C#

districtData[i] = new Person() { District = int.Parse(parts[0]), Gender = parts[1], MaritalStatus = parts[2], Age = int.Parse(parts[3]) };

VB.NET

VB.NET

districtData[i] = new Person() With { .District = Integer.Parse(parts[0]), .Gender = parts[1], .MaritalStatus = parts[2], .Age = Integer.Parse(parts[3]) }

有与此code,如果你有超过900行数据,你会得到一个索引超出范围的异常的危险。避免这种情况的一种方法是修改code我while循环来检查目标磁盘阵列(S)的边界放在上面或线条的数量都没有超过,像这样的:

There's a risk with this code that if you have more than 900 lines of data, you will get an index out of range exception. One way to avoid this would be to modify the code I put above with a while loop that checks the bounds of the target array(s) or the the number of lines haven't been exceed, like this:

C#

string[] lines = File.ReadAllLines("census.txt");
int i = 0;

while (i < 900 && i < parts.Length)
{

    // split logic goes here
}

VB.NET

VB.NET

Dim lines As String() = File.ReadAllLines("census.txt")
Dim i As Integer = 0

While (i < 900 AndAlso i < lines.Length)

    ' split logic goes here
End While

我没有测试code,但这种希望能帮助你,如果你的必须使用数组。

这篇关于从文本文件转换成并行阵列拆分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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