해시 테이블을 사용하여 파일에서 데이터를 읽고 클러스터를 만듭니다. 내 해시 테이블이 작동하지 않습니다.
는 파일의 데이터가 말해 :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로 캐스트 할 필요가 없습니다. –
다른 모든 것을 제쳐두고 비 제너릭 컬렉션을 사용하는 이유가 있습니까? –
나는 0을 캐스팅하지 않지만이 섹션은 어떻게됩니까? foreach (해시 테이블의 DictionaryEntry 항목) {for (int i = 0; i <(int) 해시 테이블 [레코드]; i ++) { Console.WriteLine (entry); } } –