本帖最后由 樊占江 于 2012-8-7 22:31 编辑
import java.util.ArrayList;
@SuppressWarnings("rawtypes")
public class LoopListTest extends ArrayList{
private static final long serialVersionUID = 1L;
static int size = 0;
public static void main(String[] args) {
LoopListTest lp = new LoopListTest(3);
lp.addOB("one");
lp.addOB("two");
lp.addOB("three");
lp.pri(lp);
System.out.println("之后-------------");
lp.addOB("four");
lp.addOB("five");
lp.pri(lp);
}
@SuppressWarnings("static-access")
LoopListTest (int size) {
this.size = size;
}
public void pri (LoopListTest lp) {
for (int i = 0; i < lp.size(); i++) {
System.out.println(lp.get(i));
}
}
@SuppressWarnings("unchecked")
public boolean addOB (Object ob) {
if (this.getMSize() >= size) {
this.remove(0);
}
return this.add(ob);
}
public Object getFirst () {
if ((size > 0) && (this.getMSize() > 0)) {
return this.get(0);
} else
return null;
}
public Object getChildAt (int item) {
if ((size > item) && (this.getMSize() > item)) {
return this.get(item);
} else
return null;
}
public int getMSize () {
return this.size();
}
}
这是全部代码 这是运行结果:
one
two
three
之后-------------
three
four
five
唯一的疑惑就是不能泛型 也不知道为什么继承于list之后不能泛型了。谁会帮忙看看吧
|
|