سؤال

ولدي بعض التعليمات البرمجية التي بملء جدول هاش بسؤال كمفتاح و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());
            }
        }
هل كانت مفيدة؟

المحلول

وكما كنت تستخدم 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 (دخول DictionaryEntry في جدول هاش) {

و}  البحث عن المزيد في http://www.dotnetperls.com/hashtable

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top