2011-01-28 47 views
0

我正在使用散列表從文件讀取數據並創建羣集。我的散列表不起作用

說在文件中的數據是:

umair,i,umair 
sajid,mark,i , k , i 

輸出是這樣的:

[{umair,umair},i] 
[sajid,mark,i,i,k] 

但我的代碼不能正常工作。下面是代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Collections; 
namespace readstringfromfile 
{ 

    class Program 
    { 
     static void Main() 
     { 
      /* int i = 0; 
      foreach (string line in File.ReadAllLines("newfile.txt")) 
      { 
       string[] parts = line.Split(','); 
       foreach (string part in parts) 
       { 
        Console.WriteLine("{0}:{1}", i,part); 
       } 
       i++; // For demo only 
      }*/ 
      Hashtable hashtable = new Hashtable(); 

      using (StreamReader r = new StreamReader("newfile.txt")) 
      { 
       string line; 
       while ((line = r.ReadLine()) != null) 
       { 
        string[] records = line.Split(','); 
        foreach (string record in records) 
        { 
         if (hashtable[records] == null) 
          hashtable[records] = (int)0; 

         hashtable[records] = (int)hashtable[records] + 1; 
         Console.WriteLine(hashtable.Keys); 

        } 
/////this portion is not working///////////////////////////////////// 

        foreach (DictionaryEntry entry in hashtable) 
        { 
         for (int i = 0; i < (int)hashtable[records]; i++) 
         { 
          Console.WriteLine(entry); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

您不需要將文字0轉換爲int。 – 2011-01-28 06:25:21

回答

2

你與records陣列插入時到哈希表(和讀取的時候),而不是使用foreach變量record工作。此外,在最終的樣子中,您基於records而不是當前的entry.Key進行迭代。您還將散列表聲明的範圍過寬,導致將所有行插入到相同的散列表中,而不是每行中的一個。

public static void Main() { 
    var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" }; 
    foreach (var line in lines) { 
     var hashtable = new Hashtable(); 
     var records = line.Split(','); 

     foreach (var record in records) { 
      if (hashtable[record] == null) 
       hashtable[record] = 0; 

      hashtable[record] = (Int32)hashtable[record] + 1; 
     } 

     var str = ""; 
     foreach (DictionaryEntry entry in hashtable) { 
      var count = (Int32)hashtable[entry.Key]; 
      for (var i = 0; i < count; i++) { 
       str += entry.Key; 
       if (i < count - 1) 
        str += ","; 
      } 
      str += ","; 
     } 

     // Remove last comma. 
     str = str.TrimEnd(','); 

     Console.WriteLine(str); 
    } 

    Console.ReadLine(); 
} 

但是,你應該考慮使用通用Dictionary<TKey,TValue>類,並使用StringBuilder如果您正在構建很多字符串。

public static void Main() { 
    var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" }; 
    foreach (var line in lines) { 
     var dictionary = new Dictionary<String, Int32>(); 
     var records = line.Split(','); 

     foreach (var record in records) { 
      if (!dictionary.ContainsKey(record)) 
       dictionary.Add(record, 1); 
      else 
       dictionary[record]++; 
     } 

     var str = ""; 
     foreach (var entry in dictionary) { 
      for (var i = 0; i < entry.Value; i++) { 
       str += entry.Key; 
       if (i < entry.Value - 1) 
        str += ","; 
      } 
      str += ","; 
     } 

     // Remove last comma. 
     str = str.TrimEnd(','); 

     Console.WriteLine(str); 
    } 

    Console.ReadLine(); 
} 
0

您試圖對一個序列的元素進行分組。 LINQ有一個內置的操作符;它被用來作爲group ... by ... into ...或等效的方法.GroupBy(...)

這意味着你可以編寫代碼(不包括文件I/O等):

var lines = new[] { "umair,i,umair", "sajid,mark,i,k,i" }; 
foreach (var line in lines) { 
    var groupedRecords = 
     from record in line.Split(',') 
     group record by record into recordgroup 
     from record in recordgroup 
     select record; 

    Console.WriteLine(
     string.Join(
      ",", groupedRecords 
     ) 
    ); 
} 

如果您喜歡短代碼迴路可以等效寫成:

foreach (var line in lines) 
    Console.WriteLine(string.Join(",", 
     line.Split(',').GroupBy(rec=>rec).SelectMany(grp=>grp))); 

兩個版本將輸出...

umair,umair,i 
sajid,mark,i,i,k 

請注意,你真的不應該使用Hashtable - 這幾乎是所有目的,這只是一個類型不安全的緩慢版本Dictionary。另外,你提到的輸出示例包括[]{}個字符 - 但是你沒有指定它們應該如何或是否應該包含在內,所以我將它們排除了。

一個LINQ組只不過是一個元素序列(這裏是相同的字符串)與一個Key(這裏是一個字符串)。因此調用GroupBy將記錄序列轉換爲一組組序列。但是,您想簡單地連接這些組。 SelectMany就是這樣一種連接:從一系列項目中,它將每個項目的「內容」連接成一個大的序列。