0

我正在编写用于显示时间与大小的代码。我正在使用哈希图进行条目。问题是它正确地进行了计算,但最后当我尝试打印出哈希图时,它不会显示 7 分钟内的所有结果。而是显示最后输入的结果7 次。我已经尝试了很多来找出问题,但不能。请帮助我

这是我的代码

public class Analysis
{
    public static class KeepTime
    {
        String time;
        long TotalSize;
    }

    Main f1 = new Main();
    public static int j = 0;
    public static int check = 0;
    public HashMap<String, KeepTime> hMap_time = new HashMap<String, KeepTime>();
    public KeepTime TimerInstance = new KeepTime();

    void TimeBasedReporting()
    {
        String skey;
        String key;
        int k;
        int y;

        for (k = 1; k <= f1.Flows().size(); k++)
        {
            check = 0;
            j = 0;

            skey = Integer.toString(k);

            if (hMap_time.isEmpty())
            {
                //some code
                key = Integer.toString(hMap_time.size() + 1);
                hMap_time.put(key, TimerInstance);
                j = 1;
            }
            else if (check == 0 && j == 0)
            {
                for (y = 1; y <= hMap_time.size(); y++)
                {
                    //some code
                    }
                }
            }

            if (check == 0 && j == 0)
            {
                // some code
                key = Integer.toString(hMap_time.size() + 1);
                hMap_time.put(key, TimerInstance);
            }
            else
            {}


        }



    }
}

以下是输出

          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
          03:08:39,AM  424
4

3 回答 3

2

You're creating a single instance of KeepTime and using a reference to that object as the value in various key/value pairs.

You should be creating a new instance each time you add a new entry, if you want them to be independent. (And yes, I agree with the comment that you almost certainly don't want a map here - but you'd get the same problem if you added the same reference to a list multiple times, too.)

于 2012-04-15T12:25:16.107 回答
1

The problem is that you are using the one TimerInstance and place that in the map for each key

You associate the same object with all keys, so of course you wil have the same value stored in the map.

Try:

KeepTime timer = new KeepTime();

at the top of the for loop and replace all TimerInstances with timer.

You can do this without fear of changing your logic as you re-assing all members of the original TimerInstance every time you store it in the map.

Also, if you want the entries in ascending order, HashMap gives you the wrong order as it is ordered based on the hash code of the keys (String in this case). Use a TreeMap if you need proper ordering -- the default string comparison results in alphabetical ordering, or you can specify your own custom Comparator to provide the ordering you require.

于 2012-04-15T12:25:43.183 回答
0

遍历映射条目的方法是遍历它的map.keySet()or map.entrySet()。使用其中之一。

于 2012-04-15T12:23:01.003 回答