字典<k,v>始终返回最后一项</k,v>

时间:2015-01-18 20:42:22

标签: c# dictionary

我在从我创建的字典中检索数据时遇到问题。字典的键是一个整数,值为List,包含Tuple

字典中有29个项目,整数键0到28.但是,无论我使用哪个键从字典中检索数据,返回的项目总是键28的字符,即字典中的最后一项。

SignalRoutes是字典的名称,SignalRoutes[i]始终返回SignalRoutes[28]

Item2

Tuple与键具有相同的值,因此结果显示当我使用1的键(给我1的键的路由)时,返回的结果是那些关键是28。

我确信我没有用相同数据的29份副本加载字典。

Debug.WriteLine("SignalRoutes count=" + SignalRoutes.Count);
for (int i = 0; i < SignalRoutes.Count; i++)
{
    if (SignalRoutes.ContainsKey(i))
        Debug.WriteLine("dict key found=" + i);
    else
        Debug.WriteLine("dict key NOT found=" + i);

    List< Tuple<int,int,int,int[]> > nsr = SignalRoutes[i];

    if (nsr != null)
    {
        for (int j = 0; j < nsr.Count; j++)
        {
            Tuple<int, int, int, int[]> r = nsr[j];
            Debug.WriteLine("route from=" + r.Item2
                + ", to=" + r.Item3
                + ", len=" + r.Item4.Length
                + ", i=" + i
                + ", j=" + j
                + ", route cntr=" + r.Item1
                );
        }
        Debug.WriteLine(String.Empty);
    }
}

这是输出的开始:

SignalRoutes count=29
dict key found=0
dict key found=1
route from=28, to=27, len=43, i=1, j=0, route cntr=51
route from=28, to=27, len=43, i=1, j=1, route cntr=52

dict key found=2
route from=28, to=27, len=43, i=2, j=0, route cntr=51
route from=28, to=27, len=43, i=2, j=1, route cntr=52

dict key found=3
route from=28, to=27, len=43, i=3, j=0, route cntr=51
route from=28, to=27, len=43, i=3, j=1, route cntr=52

...

以下是将数据加载到字典中的代码:

Stack<int> route = new Stack<int>();    
List< Tuple<int,int,int,int[]> > routes = new List< Tuple<int,int,int,int[]> >();

foreach (Signal s in SignalList)
{
    ...
    route.Clear();        
    routes.Clear();
    ...
    while ( ... )
    {

        // save route
        if (route.Count > 0)
        {
            int [] routearray = route.ToArray(); // to - from order
            Array.Reverse(routearray); // from - to order
            routecntr++;
            Tuple<int,int,int,int[]> ssroute
                = new Tuple<int,int,int,int[]>(routecntr, s.Number, s2.Number, routearray);
            routes.Add(ssroute); // add route to collection for current signal s

            int ridx = routes.Count -1;
            Debug.WriteLine("fr=" + routes[ridx].Item2
                + ", to=" + routes[ridx].Item3
                + ", len=" + routes[ridx].Item4.Length
                + ", route cntr="+routes[ridx].Item1
                );
         } // if
    } // while

    ...
    Debug.WriteLine("s="+s.Number
        +", no of routes=" + routes.Count
        );
    Debug.WriteLine(String.Empty);

    if (routes.Count == 0)
        SignalRoutes.Add(s.Number, null);
    else
        SignalRoutes.Add(s.Number, routes);
}

以下是加载的数据:

s=0, no of routes=0

fr=1, to=2, len=13, route cntr=0
s=1, no of routes=1

fr=2, to=7, len=41, route cntr=1
fr=2, to=24, len=54, route cntr=2
fr=2, to=12, len=93, route cntr=3
fr=2, to=13, len=100, route cntr=4
fr=2, to=5, len=40, route cntr=5
s=2, no of routes=5

fr=3, to=23, len=16, route cntr=6
fr=3, to=0, len=26, route cntr=7
s=3, no of routes=2

fr=4, to=3, len=14, route cntr=8
s=4, no of routes=1

...

0 个答案:

没有答案