考虑实现一个自排序的list,增加元素的时候,实现自动排序。
因为要实现一个自排序的list,所以肯定需要大量的随机插入,应该用链表式的linkedlist。考虑到排序的性能问题,该list暂定最大容纳20个元素。当继续新增时,就要删除最后一个元素
首先,linkedlist现成代码摆在那,应该复用,所以该list应该extends linkedlist。 其次,该listu应该不允许add之外的其他增加元素的方法。该list的add方法,应该只能增加实现了Comparable的对象(因为要排序)。
初步想这么多, 然后写出来代码:
package otherTest; import java.util.Collection; import java.util.LinkedList; import exception.ExceptionTest1; /** * @ClassName: MyArrayList * @Description: 私人定制arraylist * @author xuejupo [email protected] * @date 2015-11-15 下午10:57:17 */ public class MyList<T> extends LinkedList<T> { /** * @Fields serialVersionUID : 版本控制 */ private static final long serialVersionUID = 1L; //自定义list的最大值,不可改变 private final static int Max = 20; /** * @Title: getMax * @Description: 获取Mylist最大容量 * @return * @return int 返回类型 */ public static int getMax(){ return Max; } @Override public void addFirst(T e) { // TODO Auto-generated method stub throw new ExceptionTest1("the list can not addFirst",-10,"otherTest.MyList"); } @Override public void addLast(T e) { // TODO Auto-generated method stub throw new ExceptionTest1("the list can not addLast",-10,"otherTest.MyList"); } public boolean add(T e) { // TODO Auto-generated method stub if(e == null){ throw new ExceptionTest1("the list can not add null",-10,"otherTest.MyList"); } Comparable<? super T> c = (Comparable<? super T>) e; return this.add(c); } /** * @Title: add * @Description: 新增对象必须实现了 Comparable接口 * @param e * @return * @return boolean 返回类型 */ public boolean add(Comparable<? super T> e) { // TODO Auto-generated method stub if(e == null){ throw new ExceptionTest1("the list can not add null",-10,"otherTest.MyList"); } /** * 如果当前list已经满了,那么去掉最后一个(最好去最后一个,因为如果去第一个需要 * 把当前list统一前移) */ if(this.size() >= Max){ super.remove(Max - 1); } //e应该在的位置 int i = 0; while(i < this.size() && e.compareTo(this.get(i)) > 0){ i++; } if(i == this.size()){ return super.add((T)e); } /** * 不允许新增相等对象,这里也可以抛错 */ if(e.compareTo(this.get(i)) == 0){ return false; } super.add(i, (T) e); return true; } @Override public boolean addAll(Collection<? extends T> c) { // TODO Auto-generated method stub throw new ExceptionTest1("the list can not addAll",-10,"otherTest.MyList"); } @Override public boolean addAll(int index, Collection<? extends T> c) { // TODO Auto-generated method stub throw new ExceptionTest1("the list can not addAll",-10,"otherTest.MyList"); } @Override public void add(int index, T element) { // TODO Auto-generated method stub throw new ExceptionTest1("the list can not add(int,T)",-10,"otherTest.MyList"); } }
做一下简单的测试:
首先新增10个元素然后遍历:
MyList<Integer> m = new MyList<Integer>(); for(int i = 0; i < 10; i++){ m.add(new Random().nextInt(100)); } Iterator<Integer> i = m.iterator(); while(i.hasNext()){ System.out.print(i.next()+"\t"); }
结果为:
29 31 38 40 57 67 74 80 87 88
可以看到是已经排序好的。 然后使用其他方法新增:
MyList<Integer> m = new MyList<Integer>(); m.addFirst(1);
Exception in thread "main" 错误发生在:otherTest.MyList 错误发生的时间为:Mon Nov 16 00:14:30 CST 2015 错误发生的信息为:the list can not addFirst at otherTest.MyList.addFirst(MyList.java:37) at otherTest.MyListTest.main(MyListTest.java:24)
发现报错了。 看看这个自定义的exception是不是很清晰。
然后测试新增100个元素,发现结果:
for(int i = 0; i < 100; i++){ m.add(i); } 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 99
这是因为我们的定义里面,如果发现了元素个数超出,就删掉最后一个元素造成的。
简单的实现了一个自定义的list,实现一下自己的功能。但是,菜鸟新手,改java自己的方法,难免出纰漏。比如这个list里面,删除我没考虑到,可能还有其他的一些场景没考虑到,但毕竟是个学习的过程。
大家测试源码也可以这样,如果想测试源码的某个方法,extends 你想测试的类,然后修改你想测试的方法即可,当然,也可以把源码完全拷贝下来,一步一步调试看看源码怎么走的,都是不错的学习过程。