C#Foreachループハッシュテーブルの問題
質問
質問をキーとして、回答の配列リストを値としてハッシュテーブルに入力するコードがあります。
その後、ハッシュテーブルからこれらの値を出力して、ハッシュテーブル内の個々の質問の質問と対応するソリューションを表示するようにします。
ハッシュテーブルの内容を出力するためにforeachループでまったくばかげたことをしたことは知っていますが、私はコーディングを数時間続けており、ネストされた配列リストを出力するロジックは考えられません。
ヘルプは大歓迎です。
コードは次のとおりです:
//Hashtable Declaration
static Hashtable sourceList = new Hashtable();
//Class For Storing Question Information
public class QuestionAnswerClass
{
public string simonQuestion;
public ArrayList simonAnswer = new ArrayList();
}
//Foreach loop which populates a hashtable with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult)
{
Debug.WriteLine(v.question);
newques.simonQuestion = v.question;
//Debug.WriteLine(v.qtype);
//newques.simonQType = v.qtype;
foreach (var s in v.solution)
{
Debug.WriteLine(s.Answer);
newques.simonAnswer.Add(s.Answer);
}
}
sourceList.Add(qTextInput,newques);
//foreach loop to print out contents of hashtable
foreach (string key in sourceList.Keys)
{
foreach(string value in sourceList.Values)
{
Debug.WriteLine(key);
Debug.WriteLine(sourceList.Values.ToString());
}
}
解決
LINQを使用しているため、フレームワーク1.1に制限されていないことは明らかです。したがって、 HashTable
クラスと ArrayList
クラスを使用しないでください。代わりに、厳密に型指定された汎用の Dictionary
および List
クラスを使用する必要があります。
Dictionary
があるので、質問と回答を保持するクラスは必要ありません。クラスは、実際の目的のない追加のコンテナにすぎません。
//Dictionary declaration
static Dictionary<string, List<string>> sourceList = new Dictionary<string, List<string>>();
//Foreach loop which populates a Dictionary with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult) {
List<string> answers = v.solution.Select(s => s.Answer).ToList();
sourceList.Add(v.question, answers);
}
//foreach loop to print out contents of Dictionary
foreach (KeyValuePair<string, List<string>> item in sourceList) {
Debug.WriteLine(item.Key);
foreach(string answer in item.Value) {
Debug.WriteLine(answer);
}
}
何らかの理由でクラスが必要な場合、以下のようになります。
(質問の文字列はクラスで参照され、辞書のキーとして使用されますが、辞書のキーはこのコードでは実際には使用されません。)
//Class For Storing Question Information
public class QuestionAnswers {
public string Question { get; private set; }
public List<string> Answers { get; private set; }
public QuestionAnswers(string question, IEnumerable<string> answers) {
Question = question;
Answers = new List<string>(answers);
}
}
//Dictionary declaration
static Dictionary<string, QuestionAnswers> sourceList = new Dictionary<string, QuestionAnswers>();
//Foreach loop which populates a Dictionary with results from
//a linq query that i need to print out.
foreach (var v in linqQueryResult) {
QuestionAnswers qa = new QuestionAnswers(v.question, v.solution.Select(s => s.Answer));
sourceList.Add(qa.Question, qa);
}
//foreach loop to print out contents of Dictionary
foreach (QustionAnswers qa in sourceList.Values) {
Debug.WriteLine(qa.Question);
foreach(string answer in qa.Answers) {
Debug.WriteLine(answer);
}
}
他のヒント
これを試してください
foreach (DictionaryEntry entry in sourceList)
{
Debug.WriteLine(entry.Key);
foreach (object item in (ArrayList)entry.Value)
{
Debug.WriteLine(item.ToString());
}
}
軽微な調整
foreach (string key in sourceList.Keys)
{
Console.WriteLine(key);
foreach(string value in sourceList[key])
{
Console.WriteLine("\t{0}", value); // tab in answers one level
}
Console.WriteLine(); // separator between each set of q-n-a
}
これはすべきではありません:
Debug.WriteLine(sourceList.Values.ToString());
これですか?
foreach(var obj in sourceList.Values)
Debug.WriteLine(obj);
最初に、強く型付けされたジェネリックコレクションはそれを簡単にします。強く型付けされたコレクションのエイリアスを定義することから始めましょう:
using MyHash = System.Collections.Generic.Dictionary<string,
System.Collections.Generic.List<string>>;
これ以降、MyHashは長い一般的な定義と同じ意味になります。これで、ハッシュテーブルメンバーを次のように宣言できます:
static MyHash sourceList = new MyHash();
そして次のように繰り返します:
foreach (var pair in sourceList)
{
var question = pair.Value;
Console.WriteLine(question);
foreach (var answer in pair.Value)
Console.WriteLine(" " + answer);
}
これが役立つことを願っています。
foreach(HashtableのDictionaryEntryエントリ) {
} 詳細については、 http://www.dotnetperls.com/hashtable
をご覧ください。