2016-11-05 9 views
2

2つのセットを作成してリストに入れ、リスト内のすべてのアイテムを表示しようとしています。私のコードでエラーが発生しています。 Error:System.Collections.Generic.List 1[System.Collections.Generic.SortedSet 1 [System.String]] 私のコードを以下に添付します。どんな助けもありがとうございます。リスト内のすべての文字列を表示

namespace Prog 5 
    { 
     class Program 
     {   
    static void Main(string[] args) 
    { 
     List<SortedSet<string>> items = new List<SortedSet<string>>(); 
     SortedSet<string> set = new SortedSet<string>(); 
     SortedSet<string> set2 = new SortedSet<string>(); 

     set.Add("a"); 
     set.Add("b"); 
     set.Add("d"); 
     set.Add("c"); 
     set2.Add("e"); 
     set2.Add("d"); 
     set2.Add("c"); 
     foreach (string item in set) 
     { 
      items.Add(set); 
     } 
     foreach (string item in set2) 
     { 
      items.Add(set2); 
     } 
     DisplayItem(items); 

    } 

    public static void DisplaySet(SortedSet<string> set) 
    { 

     string set1 = string.Join(",", set); 
     Console.WriteLine(set1); 
     Console.ReadLine(); 
    } 

    public static void DisplayItem(List<SortedSet<string>> items) 
    { 
     foreach (SortedSet<string> item in items) 
     { 
      Console.WriteLine(items); 
      Console.ReadLine(); 
     } 
    } 
     } 

答えて

0

SortedSet<string>Objectから継承し、ToStringメソッドのデフォルトの動作()コンソールでSystem.Collections.Generic.List1[System.Collections.Generic.SortedSet1[System.String]]を受けることがあるため、GetType().ToString();です。 Reference Code

public virtual String ToString() 
{ 
    return GetType().ToString(); 
} 

SOLUTION:コンソールでSortedSetの要素を表示したい場合はあなたがこれを使用する必要があります。

Console.WriteLine(string.Join(",", item.ToArray())); 

これはSortedSet内のすべての文字列を連結して、それらを表示します,セパレータのコンソール。 DisplayItem(...)では

0

あなたは>タイプ一覧ですConsole.WriteLineを(アイテム)...を持っています。 ToString()は自動的に呼び出され、Console.WriteLineが出力する文字列を生成します:これがあなたの現在のメッセージを取得する理由です。私はそれぞれの項目をコンソールに書きたいと思っています。私が正しくあなたの要件を理解している場合

public static void DisplayItem(List<SortedSet<string>> items) 
{ 
    foreach (SortedSet<string> item in items) 
    { 
     DisplaySet(item); 
    } 
} 
0

、あなたは1つのリストに2 SortedSetsをマージするために探したり、おそらく設定されています。

次のコードは、(なぜそれが働いているのフォローの説明を)動作するはずです:質問に記載されているコードで

class Program 
{ 
    static void Main(string[] args) 
    { 
     SortedSet<string> items = new SortedSet<string>(); 
     SortedSet<string> set = new SortedSet<string>(); 
     SortedSet<string> set2 = new SortedSet<string>(); 

     set.Add("a"); 
     set.Add("b"); 
     set.Add("d"); 
     set.Add("c"); 
     set2.Add("e"); 
     set2.Add("d"); 
     set2.Add("c"); 
     foreach (string item in set) 
     { 
      items.Add(item); 
     } 
     foreach (string item in set2) 
     { 
      items.Add(item); 
     } 
     DisplayItem(items); 

    } 

    public static void DisplaySet(SortedSet<string> set) 
    { 

     string set1 = string.Join(",", set); 
     Console.WriteLine(set1); 
     Console.ReadLine(); 
    } 

    public static void DisplayItem(SortedSet<string> items) 
    { 
     foreach (string item in items) 
     { 
      Console.WriteLine(item); 
     } 
     Console.ReadLine(); 
    } 
} 

主な問題は、OrderedSet<string>オブジェクトのリストを作成しようとしていることです。各OrderedSet<string>は文字列のコレクションを表しているので、これらのコレクションを繰り返し処理し、文字列を新しいコレクションに入れる必要があります。問題のコードはそれをしません。その代わりにリストにオブジェクトを追加しています。

修正したコードは、その問題を修正し、新しいOrderedList<string>に文字列を追加します。要件に応じて、どのコレクションを使用するかを決めることができます。重複を含まないコレクションをソートする必要がある場合は、新しいOrderedSet<string>を選択できますが、重複を気にしない場合は List<string>を選択することができます。

OrderedSet<string>List<string>の違いを比較したい場合は、単に「アイテム」データタイプをList<string>に変更してプログラムを実行してください。

電流出力:

a 
b 
c 
d 
e 

あなたは

List<string> items = new List<string>(); 
... 
public static void DisplayItem(List<string> items) 
{ 
... 

SortedSet<string> items = new SortedSet<string>(); 

を変更する場合は、取得します:

a 
b 
c 
d 
c 
d 
e 
関連する問題