2

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

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

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

6 回答 6

10

当您使用 LINQ 时,您显然不受框架 1.1 的限制,因此您不应该使用HashTableandArrayList类。您应该改用严格类型的泛型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);
   }
}
于 2009-05-02T17:45:42.007 回答
2

试试这个

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

            }
于 2009-05-02T17:29:48.113 回答
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
}
于 2009-05-02T17:30:40.517 回答
0

这不应该:

Debug.WriteLine(sourceList.Values.ToString());

是这个吗?

foreach(var obj in sourceList.Values)
    Debug.WriteLine(obj);
于 2009-05-02T17:25:31.853 回答
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);
}

希望这是有用的。

于 2009-05-02T18:20:56.433 回答
0

foreach(哈希表中的字典条目){

在http://www.dotnetperls.com/hashtable中查找更多信息

于 2013-08-28T11:33:31.940 回答