在格式化文件数据中保存二维数组邻接矩阵

时间:2013-12-02 02:01:12

标签: c#

有一个文件.txt,如

 0 -> 2
 1 -> 3
 2 -> 1
 3 -> 0,4
 6 -> 3,7
 7 -> 8
 8 -> 9
 9 -> 6

我想将它存储在我正在进行的二维数组中

public static int[,] LoadFromFile(string filename)
{
    int row,column;
    int[,] edges;
    char[] splitChars = { '->' };
    using (var reader = new StreamReader(filename))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var elems = line.Split(splitChars);
            if (elems.Length <= 1)
                break;
            row = int.Parse(elems[0]);

            //think a for loop with split elements will be enough 
            //for(int aux=0; aux< lengthSplit; aux++) {
                column = int.Parse(elems);
                edges[row][column] = 1;
                edges[column][row] = 1;
            //}
        }
    }
    return edges;
}

我也在考虑使用string [] split = strings .Split(new Char [] {'->' , ',' }); 如何获得正确的输出

 0 0 1 1 0 0 0 0 0 0   (0)
 0 0 1 1 0 0 0 0 0 0   (1)
 1 1 0 0 0 0 0 0 0 0   (2)
 1 1 0 0 1 0 1 0 0 0   (3)
 0 0 0 1 0 0 0 0 0 0   (4)
 0 0 0 0 0 0 0 0 0 0   (5)
 0 0 0 1 0 0 0 1 0 1   (6)
 0 0 0 0 0 0 1 0 1 0   (7)
 0 0 0 0 0 0 0 1 0 1   (8)
 0 0 0 0 0 0 1 0 1 0   (9)

2 个答案:

答案 0 :(得分:1)

我会跟随以下内容:

  • 首先读取所有对Dictionary<int, List<int>>,否则您不知道edges数组应该有多大
  • 创建大小合适的edges数组
  • 将所有对从字典移动到数组
public static int[,] LoadFromFile(string filename)
{
    var pairs = new Dictionary<int, List<int>>();

    using (var reader = new StreamReader(filename))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var elems = line.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries);
            if (elems.Length <= 1)
                break;

            var froms = elems[0].Split(',').Select(x => int.Parse(x)).ToList();
            var tos = elems[1].Split(',').Select(x => int.Parse(x)).ToList();

            foreach (var from in froms)
            {
                if (!pairs.ContainsKey(from))
                    pairs.Add(from, new List<int>());

                foreach (var to in tos)
                {
                    if (!pairs.ContainsKey(to))
                        pairs.Add(to, new List<int>());

                    pairs[from].Add(to);
                    pairs[to].Add(from);
                }
            }
        }
    }

    var size = pairs.Keys.Max() + 1;

    var edges = new int[size, size];

    foreach(var p in pairs.SelectMany(x => x.Value.Select(y => new { x = x.Key, y})))
    {
        edges[p.x, p.y] = 1;
    }

    return edges;
}

它创建对称数组,就像样本输入/输出中的数组一样。

顺便说一下,'->'不是有效的char。这就是为什么你必须使用String.Split方法和string[], StringSplitOptions参数。

答案 1 :(得分:1)

样本第3行:

int[,] edges = new int[9, 9];
string line = "3 -> 0,4";

string[] elems = line.Split(new string[] { "->", "," }, StringSplitOptions.None);

for (int i = 0; i < elems.Count()-1; i++)
{
    int col1 = int.Parse(elems[0]);
    int col2 = int.Parse(elems[i + 1]);

    edges[col1, col2] = 1;
    edges[col2, col1] = 1;
}