ArrayList如何实现插入的数据按自定义的方式有序存放
class MyBean implements Comparable{
public int compareTo(Object obj){
if (! obj instanceof MyBean)
throw new ClassCastException() //具体异常的名称,要查jdk文档。
MyBean other = (MyBean) obj;
Return age > other.age?1:age== other.age?0:-1;
}
}
class MyTreeSet {
private ArrayList datas = newArrayList();
public void add(Object obj){
for(int i=0; i<datas.size() ; i++) {
if(obj.compareTo(datas.get(i) != 1){
datas.add(i,obj);
}
}
}
}