黑马程序员技术交流社区

标题: Map中加强for循环迭代报错 求解 [打印本页]

作者: 宗士为    时间: 2012-5-10 18:03
标题: Map中加强for循环迭代报错 求解
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;
        }
        
}

作者: 魏涞    时间: 2012-5-10 18:17
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;
         }
         
}

作者: 杨威    时间: 2012-5-10 18:47
    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方法应该也可以,然后再用“+”相连接打印就问题了
作者: 黄坚声    时间: 2012-5-10 21:36
你向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);

作者: 黄坚声    时间: 2012-5-10 21:38
问题不在for循环,而是你放进Map集合的元素。建议你好好理解Map集合。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2