2013-04-07 121 views
0

我的教授給這個類提供了一個可用於從文本文件中拆分數據的C#示例。我正在嘗試將它用於涉及分割txt內容的項目。文件分爲4個數組或字段。下面是代碼:將文本文件中的數據拆分爲並行數組

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 

它應該是假設的人口普查數據按年齡,性別,婚姻狀況,和地區去。我一直得到的輸出是單行中的每個數字或字符,如下所示:

21 

f 

s 

14 

41 

f 

m 

22 

等等。

我認爲這意味着它的工作,但我想知道如何使用它進入4個並行陣列。我也想更多地瞭解它分爲4個領域,結構或類。該項目的下一部分涉及每次出現某個年齡段或地區號碼時進行計數,這將涉及大量陣列。

+1

我不明白這背後的目的... – 2013-04-07 09:00:51

+0

鏈接海報以前的問題['我不知道我在做什麼(管理數據的多個陣列,並且每個從一個txt某個數據出現的時間計數。文件)[關閉]'](http://stackoverflow.com/questions/15859398/i-have-no-idea-what-i-am-doingmanaging-multiple-arrays-of-data-and-counting-eac#comment22573331_15859398 )上下文 – Tim 2013-04-07 17:58:55

回答

0

(如這裏的其他2個電流答案使用)泛型列表是最好的一段路要走。但是,如果您需要在陣列中的數據(如您以前question似乎表明),那麼你可以修改你的教授的這樣的代碼:

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):

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 

你也可以使用一個structclass,並有一個陣列,其保存的對象,但它看起來像你的教授要你使用4個獨立的陣列。如果你可以使用一個,你可以簡單的聲明是這樣的陣列,例如:

C#

Person[] districtData = new Person[900]; 

VB.NET

Dim districtData() As New Person(900) 

然後,你可以做到這一點,分裂邏輯內(請注意,如果說分佈和年齡是你的對象中的整數,則必須按照下面的示例進行投射或解析):

C#

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

VB.NET

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

有與此代碼,如果你有900多條數據線,你會得到一個索引超出範圍異常的風險。避免這種情況的一種方法是修改上面的代碼,使用while循環來檢查目標數組的邊界或者行數沒有超過,如下所示:

C#

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

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

    // split logic goes here 
} 

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 

我沒有測試的代碼,但這個希望能幫助你,如果你必須使用數組。

+0

謝謝!我回頭看,我不必使用平行數組,但我不知道如何將結構放入pseduocode或流程圖中,所以我認爲我會堅持使用數組。如果我使用類,我應該把循環計算一次char或int在類或主程序中出現的次數嗎?也是C#中的頭號問題,給我一個「不能隱式轉換字符串[]爲int []」的錯誤。 – Sabotenderizer 2013-04-08 02:30:26

+0

哎呀..我的不好。我編輯了代碼,以便數組正確(int或string,具體取決於),並將文件中的文本轉換爲int(必要時)。 – Tim 2013-04-08 02:34:34

+0

我會把方法用於計算一個字符或整數在主程序中出現的次數。主程序是要在其中創建您的收藏類 - 個人類本身不會知道令牌出現了多少次收藏。 – Tim 2013-04-08 02:39:02

0

你可以做一個結構所需的信息:

public struct Info 
{ 
    public int Age; 
    public string gender; 
    public string status; 
    public int district; 
} 

,並插入數據到您的結構清單:

List<Info> info = new List<Info>(); 
    foreach (string line in File.ReadAllLines("census.txt")) 
    { 
     string[] parts = line.Split(','); 

      info.Add(new Info() {Age=int.Parse(parts[0]), gender=parts[1], status=parts[2], district=int.Parse(parts[3]) }); 
    } 

現在喲擁有者信息的列表。

1

我將延長irsog的回答有點:

  • 使用,而不是一個結構類
  • ,而是使用屬性字段
  • 使用GenderMaritalStatus枚舉,而不是簡單的字符串

代碼:

public class Person 
{ 
    public int Age { get; set; } 
    public MaritalStatus MaritalStatus { get; set; } 
    public Gender Gender { get; set; } 
    public int District { get; set; } 
} 

public enum MaritalStatus 
{ 
    Single, Married 
} 

public enum Gender 
{ 
    Male, Female 
} 

與用法:

var people = new List<Person>(); 

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

    people.Add(new Person() { 
     Age = int.Parse(parts[0]), 
     MaritalStatus = parts[1] == "s" ? MaritalStatus.Single : MaritalStatus.Married, 
     Gender = parts[2] == "m" ? Gender.Male : Gender.Female, 
     District = int.Parse(parts[3]) 
    }); 
} 
相關問題