C# C Foreach循环哈希表问题

C# C Foreach循环哈希表问题,c#,hashtable,foreach,C#,Hashtable,Foreach,我有一些代码,它用一个问题作为键,一个答案数组列表作为值来填充哈希表 然后我想从哈希表中打印出这些值,以便它显示哈希表中每个问题的问题和相应的解决方案 我知道我对foreach循环做了一些非常愚蠢的事情来打印哈希表的内容,但是我已经连续编码好几个小时了,我想不出打印嵌套arraylist的逻辑 非常感谢你的帮助 代码如下: //Hashtable Declaration static Hashtable sourceList = new Hashtable(); //Class For

我有一些代码,它用一个问题作为键,一个答案数组列表作为值来填充哈希表

然后我想从哈希表中打印出这些值,以便它显示哈希表中每个问题的问题和相应的解决方案

我知道我对foreach循环做了一些非常愚蠢的事情来打印哈希表的内容,但是我已经连续编码好几个小时了,我想不出打印嵌套arraylist的逻辑

非常感谢你的帮助

代码如下:

//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());
            }
        }
这不应该:

Debug.WriteLine(sourceList.Values.ToString());
是这个吗

foreach(var obj in sourceList.Values)
    Debug.WriteLine(obj);
试试这个

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
}

在使用LINQ时,显然不受framework 1.1的约束,因此不应该使用HashTable和ArrayList类。您应该使用严格类型的泛型词典和列表类

你不需要一个类来保存问题和答案,因为你有字典。该类将只是一个没有实际用途的额外容器

//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);
   }
}

首先,强类型泛型集合将使其更容易。让我们首先为强类型集合定义一个别名:

using MyHash = System.Collections.Generic.Dictionary<string,
    System.Collections.Generic.List<string>>;
然后像这样迭代:

foreach (var pair in sourceList)
{
    var question = pair.Value;
    Console.WriteLine(question);
    foreach (var answer in pair.Value)
        Console.WriteLine("    " + answer);
}

希望这是有用的。

foreach dictionary在Hashtable中输入条目 {

}

中查找更多内容看起来像是的副本。请确认并投票决定是否关闭。非常好的答案谢谢,我想我真的不应该一开始就为非类型安全解决方案而烦恼。ThankyouError1:foreach语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义
foreach (var pair in sourceList)
{
    var question = pair.Value;
    Console.WriteLine(question);
    foreach (var answer in pair.Value)
        Console.WriteLine("    " + answer);
}