黑马程序员技术交流社区
标题:
求解关于map的一个小问题
[打印本页]
作者:
张先龙
时间:
2013-4-7 10:51
标题:
求解关于map的一个小问题
本帖最后由 张先龙 于 2013-4-7 19:21 编辑
代码如下:
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 hashCode()
{
return name.hashCode()+age*35;
}
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 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 String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+"..."+age;
}
}
class MapEntryDemo
{
public static void main(String[] args)
{
HashMap<Student,String> stu= new HashMap<Student,String>();
stu.put(new Student("小遥",8),"三年级");
stu.put(new Student("小豪",6),"一年级");
stu.put(new Student("小程",2),"家里蹲");
stu.put(new Student("小明",9),"六年级");
stu.put(new Student("小露",12),"七年级");
Set<Student> keyset=stu.keySet();
Iterator<Student> it =stu.iterator();
while(it.hasNext())
{
Student key=it.next();
String grate=stu.get(key);
System.out.println(grate+key);
}
Set<Map.Entry<Student,String>> entryset=stu.entrySet();
Iterator<Map.Entry<Student,String>> iter=entryset.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me=iter.next();
System.out.println(me.getKey()+ me.getValue());
}
}
}
编译不通过啊。。
作者:
我手心里的宝
时间:
2013-4-7 11:04
把编译错误的信息贴一下 这样看起来费劲
作者:
wanggang
时间:
2013-4-7 11:14
编译不通过是因为有语法错误,将stu.iterator()改成new ArrayList().iterator();就行了
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 hashCode()
{
return name.hashCode()+age*35;
}
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 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 String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+"..."+age;
}
}
class MapEntryDemo
{
public static void main(String[] args)
{
HashMap<Student,String> stu= new HashMap<Student,String>();
stu.put(new Student("小遥",8),"三年级");
stu.put(new Student("小豪",6),"一年级");
stu.put(new Student("小程",2),"家里蹲");
stu.put(new Student("小明",9),"六年级");
stu.put(new Student("小露",12),"七年级");
Set<Student> keyset=stu.keySet();
Iterator<Student> it = new ArrayList().iterator();
;
while(it.hasNext())
{
Student key=it.next();
String grate=stu.get(key);
System.out.println(grate+key);
}
Set<Map.Entry<Student,String>> entryset=stu.entrySet();
Iterator<Map.Entry<Student,String>> iter=entryset.iterator();
while(iter.hasNext())
{
Map.Entry<Student,String> me=iter.next();
System.out.println(me.getKey()+ me.getValue());
}
}
}
作者:
黑马李超
时间:
2013-4-7 13:31
Set<Student> keyset=stu.keySet();
Iterator<Student> it =stu.iterator();//
这个stu应该改成keyset,stu是map集合,不能直接用迭代器遍历的。
作者:
陈丽莉
时间:
2013-4-7 14:27
若还有问题,继续追问; 没有的话,将帖子分类改成【已解决】~
作者:
张先龙
时间:
2013-4-7 19:21
解决了 ,,3Q各位!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2