【问题标题】:Converting a text file to graph将文本文件转换为图形
【发布时间】:2015-12-04 18:42:59
【问题描述】:

我正在尝试将文本文件转换为图形,但是当我尝试使用 values 函数(如下)打印字典中的值时,它会打印出类似“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];
    }

}
}

【问题讨论】:

    标签: c# dictionary graph text-files


    【解决方案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);
           }
        }
    

    【讨论】:

    • 谢谢!我有一个问题,如果我要创建一个打印整个字典的函数,是否可以循环每个键值对?
    猜你喜欢
    • 2011-07-09
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多