私はハッシュテーブルを使ってファイルからデータを読み込み、クラスタを作成しています。 私のハッシュテーブルdoes not work
は、ファイル内のデータがあると言う:umair,i,umair
sajid,mark,i , k , i
出力は次のようである:
[{umair,umair},i]
[sajid,mark,i,i,k]
しかし、私のコードは動作しません。ハッシュテーブルに挿入するとき(およびそれからの読み取り時)の代わりに、record
変数foreachの使用あなたがrecords
配列で作業している
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をintにキャストする必要はありません。 –
他のすべてを残しておけば、まだジェネリックでないコレクションを使用している理由はありますか? –
oki私は0をキャストしませんが、このセクションはどうですか? foreach(ハッシュテーブルのDictionaryEntryエントリ) { for(int i = 0; i <(int)ハッシュテーブル[レコード]; i ++) { Console.WriteLine(entry); } } –