黑马程序员技术交流社区
标题:
已经看了半个小时的代码 没找出来错!
[打印本页]
作者:
刘茂林
时间:
2013-5-15 23:44
标题:
已经看了半个小时的代码 没找出来错!
本帖最后由 刘茂林 于 2013-5-16 00:04 编辑
汗 实在找不出来了 都看了半个小时了。。本来想靠自己搞定的。。
/**
* Map 应用: 需求 每一个学生都有对应的归属地。 学生Student,地址String 学生属性: 姓名, 年龄 注意:姓名和年龄相同的视为同一个学生。
* 保证学生的唯一性。
*
* 1, 描述学生的唯一性。
*
* 2,定义map容器,将学生作为键,地址作为值,存入。
*
* 3,获取map集合中的元素
*
*/
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
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*34;
}
public boolean equals(Object obj)// 判断相等 复写 equals方法
{
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 toStrint()
{
return name + ":" + age;
}
}
public class MapTest
{
public static void main(String[] args)
{
/*Student st1 = new Student("liu01", 23);
Student st2 = new Student("liu02", 24);
Student st3 = new Student("liu03", 22);
Student st4 = new Student("liu04", 21);
*/
HashMap<Student, String> hm = new HashMap<Student, String>();
hm.put(new Student("liu01", 22), "beijing");
hm.put(new Student("liu03", 23), "shanghai");
hm.put(new Student("liu04", 24), "wuhan");
hm.put(new Student("liu02", 22), "nanjing");
// 第一种取出方式 keySet
Set<Student> keySet = hm.keySet();// 尖括号 泛型
Iterator<Student> it = keySet.iterator();// 这里也用到泛型
while (it.hasNext())
{
Student stu = it.next();
String add = hm.get(stu);
System.out.println(stu + "...." + add);
}
}
}
复制代码
打印的时候打印的不是 Student的 姓名和年龄 打印的是hashCode 还是 地址?
作者:
李志敏
时间:
2013-5-15 23:54
一眼看出 63行 toStrint() 写错了 应该是toString()
作者:
刘学明
时间:
2013-5-16 00:01
你的toString方法写错了 toString方法是Object类中的方法 如果没有覆盖成功则打印出来的是Student@哈希值。
有两种方法可以打印出Student的姓名和年龄。
方法1:复写toString()方法。
方法2: 在打印的时候直接调用学生类的getName()和getAge()方法。
代码示例:
/**
* Map 应用: 需求 每一个学生都有对应的归属地。 学生Student,地址String 学生属性: 姓名, 年龄 注意:姓名和年龄相同的视为同一个学生。
* 保证学生的唯一性。
*
* 1, 描述学生的唯一性。
*
* 2,定义map容器,将学生作为键,地址作为值,存入。
*
* 3,获取map集合中的元素
*
*/
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
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*34;
}
public boolean equals(Object obj)// 判断相等 复写 equals方法
{
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 toStrint() // 第一种方法: 把toStrint改成 toString
{
return name + ":" + age;
}
}
public class MapTest
{
public static void main(String[] args)
{
/*Student st1 = new Student("liu01", 23);
Student st2 = new Student("liu02", 24);
Student st3 = new Student("liu03", 22);
Student st4 = new Student("liu04", 21);
*/
HashMap<Student, String> hm = new HashMap<Student, String>();
hm.put(new Student("liu01", 22), "beijing");
hm.put(new Student("liu03", 23), "shanghai");
hm.put(new Student("liu04", 24), "wuhan");
hm.put(new Student("liu02", 22), "nanjing");
// 第一种取出方式 keySet
Set<Student> keySet = hm.keySet();// 尖括号 泛型
Iterator<Student> it = keySet.iterator();// 这里也用到泛型
while (it.hasNext())
{
Student stu = it.next();
String add = hm.get(stu);
System.out.println(stu + "...." + add);//第二种方法:改成System.out.println(stu.getName()+":"stu.getAge());
}
}
}
复制代码
作者:
刘茂林
时间:
2013-5-16 00:04
本帖最后由 刘茂林 于 2013-5-16 00:05 编辑
刘学明 发表于 2013-5-16 00:01
你的toString方法写错了 toString方法是Object类中的方法 如果没有覆盖成功则打印出来的是Student@哈希值。 ...
真失败啊。。犯了个这种错误。。。多谢了。。
没想到还有第二种方法。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2