ArraylistDemo
package cn.stat.p6.arraylist.demo; import java.util.ArrayList; import java.util.Iterator; import cn.stat.p5.person.demo.PresonDemo; public class ArraylistDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<PresonDemo> al=new ArrayList<PresonDemo>(); PresonDemo p1=new PresonDemo("list1",11); PresonDemo p2=new PresonDemo("list2",12); PresonDemo p3=new PresonDemo("list3",13); al.add(p1); al.add(p2); al.add(p3); al.add(new PresonDemo("list5",15)); Iterator<PresonDemo> it=al.iterator(); while(it.hasNext()) { PresonDemo p=(PresonDemo) it.next(); System.out.println(p.getName()+";;"+p.getAge()); } show(3); String qc="123"; String ai="12"; System.out.println(ai.hashCode()); System.out.println(qc.hashCode()); } public static void show(int i) { int s=i+1; System.out.println(s); } }
PresonDemo
package cn.stat.p5.person.demo; public class PresonDemo implements Comparable { private String name; private int age; /** * @param args */ public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void outPrint() { System.out.println(this.age+".."+this.name); } public PresonDemo(String name, int age) { this.name = name; this.age = age; } //TreeSet();为空时,实列类内部比较器 @Override public int compareTo(Object o) { // TODO Auto-generated method stub PresonDemo p=(PresonDemo)o; if(p.age<this.age) return 1; if(p.age>this.age) return-1; return 0; } }
原文:http://www.cnblogs.com/zywf/p/4743319.html