【问题标题】:sorting map of the key to values with integer and strings使用整数和字符串将键的排序映射到值
【发布时间】:2013-10-30 14:22:38
【问题描述】:

我正在尝试返回这个骰子的面到字母的映射。使用整数值 1 到 6 来识别人脸,返回的地图按其键(人脸编号)排序。 我的代码如下:

公共 SortedMap getValueMap() {

    SortedMap<Integer, String> sm = new TreeMap<Integer, String>();

    sm.put(new Integer(1), "A");
    sm.put(new Integer(2), "A");
    sm.put(new Integer(4), "E");
    sm.put(new Integer(3), "E");
    sm.put(new Integer(5), "G");
    sm.put(new Integer(6), "N");
    Set<Entry<Integer, String>> s = sm.entrySet();

    Iterator<Entry<Integer, String>> i = s.iterator();

    for (i=1; i.hasNext();) {
        Map.Entry m = (Map.Entry) i.next();
        int key = (Integer) m.getKey();
        String sValue = (String) m.getValue();

    }
    return "Key :" + key + "  value :" + sValue;

}

【问题讨论】:

  • 这里有什么问题?
  • 根据你可以看到我想要做什么。但是,代码中有一些错误,我不明白它们是什么。这就是我在这里发布问题的原因。
  • 如果您粘贴错误,其他人将能够帮助您
  • 编译器已经告诉您它在哪里检测到错误。 什么你不明白它告诉你什么?

标签: java sorting map iterator mapping


【解决方案1】:

这里不需要初始化i=1。改成while循环

while(i.hasNext())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2020-02-03
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多