C#Foreach循环哈希表问题

时间:2009-05-02 17:19:23

标签: c# hashtable foreach

我有一些代码填充哈希表,其中一个问题作为键,一个答案的arraylist作为值。

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

我知道我已经用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());
            }
        }

6 个答案:

答案 0 :(得分:10)

当您使用LINQ时,您显然不受框架1.1的约束,因此您不应该使用HashTableArrayList类。您应该使用严格类型的通用DictionaryList类。

您不需要课程来保留问题和答案,因为您拥有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);
   }
}

答案 1 :(得分:2)

试试这个

foreach (DictionaryEntry entry in sourceList)
            {
                Debug.WriteLine(entry.Key);
                foreach (object item in (ArrayList)entry.Value)
                {
                    Debug.WriteLine(item.ToString());
                }

            }

答案 2 :(得分:1)

小调整

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
}

答案 3 :(得分:0)

不应该这样:

Debug.WriteLine(sourceList.Values.ToString());
是吗?

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

答案 4 :(得分:0)

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

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

希望这很有用。

答案 5 :(得分:0)

foreach(Hashtable中的DictionaryEntry条目) {

}  在http://www.dotnetperls.com/hashtable

中查找更多内容
相关问题