我正在研究一個程序,一個用戶輸入一組字母,並在字母混亂了一下之後,他們被比較爲一個單詞列表我存儲在一個程序數據庫。由於在數據庫中搜索每個字母組合的速度非常緩慢,因此我試圖首先將這些單詞存儲在列表中,這樣它們就可以在內存中使用,從而提高了性能。但我想通過將單詞列表存儲在散列表中的單詞列表中,其中關鍵字是單詞的第一個字母,但我仍然有點困擾如何去做。存儲字典到hashtable
我有什麼(顯然是行不通的)是:
public Hashtable populateWordTable()
{
Hashtable wordTable = new Hashtable();
List<string> wordTableWordList = new List<string>();
connect = new SqlConnection(connectionString);
SqlCommand find = new SqlCommand("Select * FROM English order by Word", connect);
// starting with first record, store each word into the List<string> wordlist
SqlDataReader dr = null;
int found = 0;
try
{
connect.Open();
dr = find.ExecuteReader();
string key = string.Empty;
string keyStartingPosition = "a";
while (dr.Read())
{
// if word is present
if (!dr.IsDBNull(0))
{
found = Convert.ToInt32(dr[0]);
key = dr[1].ToString().Substring(0, 1);
}
if (found > 0)
{
// if we have transitioned to the next letter in the alphabet
if (key != keyStartingPosition)
{
wordTable.Add(keyStartingPosition, wordTableWordList);
List<string> newList = new List<string>();
newList.Add(dr[1].ToString());
keyStartingPosition = key;
}
// still in the same letter in the alphabet
else
{
wordTableWordList.Add(dr[1].ToString());
}
}
}
}
catch (Exception ex)
{
}
finally
{
connect.Close();
}
return wordTable;
}
HashTables本質上不是排序集合,而且您也不能定義關鍵字。你應該使用一個字典。 – KappaG3