2015-12-04 4 views
-1

Я пытаюсь преобразовать текстовый файл в граф, но когда я пытаюсь напечатать значения в словаре, используя функцию значений (ниже), он печатает что-то вроде «System.Collections.Generic. List '1 [System.String] "! Может ли кто-нибудь сказать мне, что не так в моем коде ?! This is the format of the text fileПреобразование текстового файла в граф

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.IO; 


namespace Demo 
{ 

public class Graph 
{ 
    Dictionary<string, List<string>> st; 
    static int count; 
    static int count1; 


    public Graph() 
    { 
     st = new Dictionary<string, List<string>>(); 
    } 
    public Graph(string filename, string delimeter) 
    { 
     st = new Dictionary<string, List<string>>(); 

     FileStream fs = new FileStream(filename, FileMode.Open); 

     StreamReader sr = new StreamReader(fs); 


     while(sr.Peek()!=-1) 
     { 
      string line = sr.ReadLine(); 
      char r; 
      r = Convert.ToChar(delimeter); 

      string [] names = line.Split(r);    

      for(int i=1;i<names.Length;i++) 
      { 
       addEdge(names[1], names[i]); 

      } 

     } 

     sr.Close(); 
    } 

    public void addEdge(string a, string b) 
    { 
     if(!st.ContainsKey(a)) 
     { 
      addVertex(a); 
     } 
     if(!st.ContainsKey(b)) 
     { 
      addVertex(b); 
     } 
     st[a].Add(b); 
     st[b].Add(a); 
     count1++; 


    } 

    public void addVertex(string v) 
    { 
     isContainV(v); 
     st.Add(v, new List<string>()); 
     count++; 
    } 
    public int vertexcount() 
    { 
     return count; 
    } 
    public int edgeCount() 
    { 
     return count1; 
    } 
    public void edge() 
    { 
     foreach (string key in st.Keys) 
     { 
      Console.WriteLine(key); 
     } 
    } 
    public void values() 
    { 
     foreach (List<string> temp in st.Values) 
     { 
      Console.WriteLine(temp); 
     } 
    } 
    public bool isContainV(string v) 
    { 
     if(!st.ContainsKey(v)) 
     { 
      return false; 
     } 
     return true; 
    } 
    public IEnumerable<string> adjacentTo(string v) 
    { 
     return st[v]; 
    } 

} 
} 

ответ

1

Вы не можете написать набор строк, вам нужно добавить еще один цикл, чтобы написать каждую строку.

foreach (List<string> temp in st.Value) 
    { 
     foreach (string s in temp) 
     { 
      Console.WriteLine(s); 
     } 
    } 

Если вы хотите просто иметь одну петлю за весь словарь попробовать это ...

foreach (KeyValuePair<string, List<string>> entry in st) 
    { 
     Console.WriteLine(entry.Key); 

     foreach (string s in entry.Value) 
     { 
      Console.WriteLine(s); 
     } 
    } 
+0

Спасибо! У меня есть вопрос, можно ли зацикливать на каждой паре ключей, если бы я должен был создать функцию, которая печатает весь словарь? – brokleyscoding

+0

Да, см. Мое редактирование. – EricM

+0

Большое вам спасибо! – brokleyscoding

Смежные вопросы