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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import java.util.*;
class MapTest2
{
public static void main(String[] args)
{
  HashMap<Student,String> hmap=new HashMap<Student,String>();
  hmap.put(new Student("张三",21),"北京");
  hmap.put(new Student("李四",18),"上海");
  hmap.put(new Student("王五",26),"广州");
  hmap.put(new Student("abc",21),"南京");
  Set<Map.Entry<Student,String>> entryset=hmap.entrySet();
  Iterator<Map.Entry<Student,String>> it=entryset.iterator();
  while(it.hasNext())
  {
   Map.Entry<Studengt,String> ma=it.next();
   Student stu=ma.getKey();
   String addr=ma.getValue();
   System.out.println(stu+"::地址:"+addr);
  }
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age)
{
  this.name=name;
  this.age=age;
}
public int compareTo(Student s)
{
  int num=new Integer(this.age).compareTo(new Integer(s.age));
  if(num==0)
   return this.name.compareTo(s.name);
  return num;
}
public int hashCode()
{
  return name.hashCode()+age*12;
}
public boolean equals(Object obj)
{
  if(!(obj instanceof Student))
   throw new ClassCastException("类型不匹配");
  Student s=(Student) obj;
  return this.name.equals(s.name) && this.age==s.age;
}
public String getName()
{
  return name;
}
public int getAge()
{
  return age;
}
public String toString()
{
  return "姓名:"+name+":年龄:"+age;
}
}


编译时,出现的错误是

1.png (2.45 KB, 下载次数: 18)

错误提示

错误提示

评分

参与人数 1技术分 +1 收起 理由
奋斗的青春 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
Map.Entry<Studengt,String> ma=it.next();
   Student stu=ma.getKey();
   String addr=ma.getValue();
   System.out.println(stu+"::地址:"+addr);

第一行的student写错了,最后一行的分号是中文的

评分

参与人数 1技术分 +1 收起 理由
奋斗的青春 + 1 赞一个!

查看全部评分

回复 使用道具 举报
颜峰 发表于 2012-11-14 12:41
Map.Entry ma=it.next();
   Student stu=ma.getKey();
   String addr=ma.getValue();

谢啦,看来真是太不小心了。。。。
回复 使用道具 举报
呵呵~
一开始我们都会 忘记 英文及中文下的标点符号  谨记即可;拼错 student 以后记得仔细哦·~ 加油!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马