一下是小弟写的Student类和关于对象的各种存储,请给位大牛给指点一下!- <p>class Student implements Comparable<Student>
- {
- private String name;
- private int age;
- private String num;
-
- public Student() {}
- public Student(String name, int age, String num)
- {
- this.name = name;
- this.age = age;
- this.num = num;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public void setNum(String num)
- {
- this.num = num;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public String getNum()
- {
- return num;
- }
-
- public void show()
- {
- System.out.println("name:"+name+" age:"+age+" num:"+num);
- }
-
- public int hashCode()
- {
- return num.hashCode();
- }
-
- public boolean equals(Object obj)
- {
- if(!(obj instanceof Student))
- throw new ClassCastException("不是Student类");
-
- Student s = (Student)obj;
- return this.getName().equals(s.getName())&&this.getAge() == s.getAge();
- }
-
- public int compareTo(Student s)
- {
- int n = this.getName().compareTo(s.getName());
- if(0 == n)
- return s.getAge() - this.getAge();
- return n;
- }
- }
- </p><div class="blockcode"><blockquote>import java.util.*;
- class MyStudentTest
- {
- public static void main(String []args)
- {
- ArrayList <Student> al = new ArrayList<Student>();
-
- al.add(new Student("bbb",13,"2001002"));
- al.add(new Student("ccc",15,"2001003"));
- al.add(new Student("aaa",12,"2001001"));
- al.add(new Student("ccc",18,"2001005"));
-
- Iterator<Student> it = al.iterator();
-
- while(it.hasNext())
- {
- it.next().show();
- }
-
- LinkedList <Student> li = new LinkedList<Student>();
-
- li.addFirst(new Student("孙悟空",11200,"2001011"));
- li.addLast(new Student("猪八戒",1300,"2001022"));
- li.addFirst(new Student("沙悟净",1400,"2001033"));
- li.addFirst(new Student("小白龙",13120,"2001007"));
-
- Iterator <Student> it2 = li.iterator();
- while(it2.hasNext())
- {
- it2.next().show();
- }
-
- HashSet <Student> hs = new HashSet<Student>();
-
- hs.add(new Student("贾宝玉",20,"2002123"));
- hs.add(new Student("薛宝钗",19,"2002234"));
- hs.add(new Student("林黛玉",18,"2002345"));
- hs.add(new Student("王熙凤",20,"2002456"));
- hs.add(new Student("林黛玉",18,"2002345"));
-
- Iterator<Student> it3 = hs.iterator();
- while(it3.hasNext())
- {
- it3.next().show();
- }
-
-
- TreeSet <Student> ts = new TreeSet<Student>();
-
- ts.add(new Student("贾宝玉",20,"2002123"));
- ts.add(new Student("薛宝钗",19,"2002234"));
- ts.add(new Student("林黛玉",18,"2002345"));
- ts.add(new Student("王熙凤",20,"2002456"));
-
- Iterator <Student>it4 = ts.iterator();
-
- while(it4.hasNext())
- {
- it4.next().show();
- }
-
-
- HashMap<String,Student> hm = new HashMap<String,Student>();
-
- hm.put("first",new Student("贾宝玉",20,"2002123"));
- hm.put("second",new Student("薛宝钗",19,"2002234"));
- hm.put("thrid",new Student("林黛玉",18,"2002345"));
- hm.put("fouth",new Student("薛宝钗",19,"2002234"));
-
-
- Set<String> set1 = hm.keySet();
- Iterator <String> it5 = set1.iterator();
- while(it5.hasNext())
- {
- String key = it5.next();
- System.out.print(key+"...");
- hm.get(key).show();
- }
- Set<Map.Entry<String,Student>> set2 = hm.entrySet();
- Iterator<Map.Entry<String,Student>> it6 =set2.iterator();
- while(it6.hasNext())
- {
- Map.Entry<String,Student> me1 =it6.next();
- System.out.print(me1.getKey()+" ");
- me1.getValue().show();
- }
-
- TreeMap <String, Student> tm = new TreeMap<String,Student>();
- tm.put("first",new Student("贾宝玉",20,"2002123"));
- tm.put("second",new Student("薛宝钗",19,"2002234"));
- tm.put("thrid",new Student("林黛玉",18,"2002345"));
- tm.put("fouth",new Student("薛宝钗",19,"2002234"));
-
- Set<String> set3 = tm.keySet();
- Iterator<String> it7 = set3.iterator();
- while(it7.hasNext())
- {
- String key = it7.next();
- System.out.print(key+" ");
- tm.get(key).show();
- }
-
- Set<Map.Entry<String,Student>> mapentry = tm.entrySet();
- Iterator<Map.Entry<String,Student>> it8 = mapentry.iterator();
- while(it2.hasNext())
- {
- Map.Entry<String,Student> me2 = it8.next();
- System.out.print(me2.getKey()+" ");
- me2.getValue().show();
- }
-
- }
-
- }
复制代码 |
|