A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 宗士为 中级黑马   /  2012-5-10 18:03  /  1858 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.cast.MapDemo;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import cn.cast.Person.Person;

public class MapDemo {

        public static void main(String[] args) {
                Map<Person, Integer> m = new TreeMap<Person, Integer>(new MyComparator());
                m.put(new Person("赵六", 21), 80);
                m.put(new Person("王五", 18), 80);
                m.put(new Person("赵六", 21), 70);
                m.put(new Person("李四", 20), 88);
                m.put(new Person("赵六", 20), 60);
                m.put(new Person("张三", 19), 85);
               
                for(Person p : m.keySet()) {
                        System.out.println(p + m.get(p));   //不明白为什么报错  请明白人解释 下为什么  怎么改
                        
                }
               
        }

}

class MyComparator implements Comparator<Person> {
        public int compare(Person o1, Person o2) {
                int ageGap = o1.getAge() - o2.getAge();
                int nameGap = o1.getName().compareTo(o2.getName());
                return nameGap != 0 ? nameGap : ageGap;
        }
        
}

评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

import cn.cast.Person.Person;

public class MapDemo {

        public static void main(String[] args) {
                 Map<Person, Integer> m = new TreeMap<Person, Integer>(new MyComparator());
                 m.put(new Person("赵六", 21), 80);
                 m.put(new Person("王五", 18), 80);
                 m.put(new Person("赵六", 21), 70);
                 m.put(new Person("李四", 20), 88);
                 m.put(new Person("赵六", 20), 60);
                 m.put(new Person("张三", 19), 85);
                 
                for(Person p : m.keySet()) {
                         System.out.println(p + m.get(p));  //这里提示的是“+”运算符不知道该进行什么样的操作,
                                              //p+m.get(p)不是一个字符串,可以简单的在“+”
                                         //一个字符串进行转换
                                                                                                 //比如:System.out.println(p +""+ m.get(p));
                 }
                 
        }

}

class MyComparator implements Comparator<Person> {
         public int compare(Person o1, Person o2) {
                 int ageGap = o1.getAge() - o2.getAge();
                 int nameGap = o1.getName().compareTo(o2.getName());
                 return nameGap != 0 ? nameGap : ageGap;
         }
         
}

评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

回复 使用道具 举报
    for(Person p : m.keySet()) {
                        System.out.println(p + m.get(p));   //不明白为什么报错  请明白人解释 下为什么  怎么改

m.get(p):是用键值去获取相应的value值,你的value值是分数,也是int型的数,你是想用“+”在语句中连接两个字符串打印出来,但是
m.get(P)结果不是String类型的,所以就报错了
Map<Person, Integer> m = new TreeMap<Person, Integer>(new MyComparator());这里你将value值封装成Integer类型,直接调用Integer的toString方法应该也可以,然后再用“+”相连接打印就问题了

评分

参与人数 1技术分 +1 收起 理由
攻城狮 + 1 赞一个!

查看全部评分

回复 使用道具 举报
你向Map集合里放进去的Person对象没有保证唯一性。

Map<Person, Integer> m = new TreeMap<Person, Integer>(new MyComparator());
                 m.put(new Person("赵六", 21), 80);//你向Map集合里放进去的Person对象没有保证唯一性。
                 m.put(new Person("王五", 18), 80);
                 m.put(new Person("赵六", 21), 70);//你向Map集合里放进去的Person对象没有保证唯一性。
                 m.put(new Person("李四", 20), 88);
                 m.put(new Person("赵六", 20), 60);
                 m.put(new Person("张三", 19), 85);
回复 使用道具 举报
问题不在for循环,而是你放进Map集合的元素。建议你好好理解Map集合。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马