编程思路是:实现一个类对ArrayList进行包装,当程序试图向ArrayList中放入数据时,程序将先检查该元素与ArrayList集合中其他元素的大小,然后将该元素插入到指定位置。
class MyBean implements Comparable{
public int compareTo(Object obj){
if(! obj instanceof MyBean)
throw new ClassCastException()。
MyBean other = (MyBean) obj;
return age > other.age?1:age== other.age?0:-1;
}
}
class MyTreeSet {
private ArrayList datas = new ArrayList();
public void add(Object obj){
for(int i=0;i<datas.size();i++){
if(obj.compareTo(datas.get(i) != 1){
datas.add(i,obj);
}
}
}
}