本帖最后由 淡淡柠檬茶 于 2014-6-5 14:45 编辑
//代码如下,编译的时候提示“解析时已到达文件结尾”,这是什么意思来的,我记得在视频中老师提示过,但是我找不到了
//练习 每一个学生都有归属地,学生属性姓名 年龄(学生姓名和年龄相同是同一个人,要保证唯一性)
import java.util.*;
class Student
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("1","c1");
map.put("2","c1");
map.put("3","c1");
map.put("4","c1");
//(1)将map集合转成 Set集合
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext())
{
String key = it.next();
String value = map.get(key);
System.out.println(key+ value);
}
/*
//(2)将map集合中的映射关系存入到set集合中
Set<Map.Entry<String,String>> entrySet = map.entrySet();
Iterator <Map.Entry<String,String>> it = entrySet.iterator();
while (it.hasNext())
{
Map.Entry<String,String> me =it.next();
String key = me.getKey();
String value = me.getValue();
System.out.println(key + value);
}
*/
}
class Person extends comparable
{
private String name;
private int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name + age;
}
public int hashcode()
{
return name.hashcode()+age*37;
}
public boolean equals(Object obj)
{
if (obj instanceof Person)
return false;
Student s = (Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
public int compareto(Object s1)
{
int num = new integer(this.name).compareTo(new integer(s1.name));
if (num =0)
return this.age.compareTo(s1.age);
return num;
}
}
|