java arraylist

来源:百度知道 编辑:UC知道 时间:2024/09/28 10:20:17
一个3个类
第一个UserClient类
用paint
public void paint(Graphics g) {
First.draw(g);
Second.draws(g);
g.drawString("boom:"+booms.size(), 20, 38);
for (int i = 0; i < 15; i++) {
g.drawLine(0, 0 + 40 * i, 600, 40 * i);
g.drawLine(40 * i , 40, 40 * i, 560);
}
for (int i=0 ;i<booms.size();i++)
{
Boom b = booms.get(i);
b.draw(g);
}
}
第二个类
实例化
public Boom boomDown(){

int x=this.x ;
int y=this.y;
Boom b=new Boom(x,y ,this.uc);
uc.booms.add(b);
// 定时器实例化
Boom.Timers tt= new Boom(x,y,this.uc).new Timers(1);
return b;
}
第3个类中的一个内部类
用于remove list中的元素
public class Timers {
public Timers(int seconds) {
timer = new Timer();
timer.schedule(new TimerTask() {public void run() {

live = false;
uc.booms.remove(this);//问题!!不能取消boom
hadBoom();
System.out.println("In TimerTestTask, execute run method.

你这个this并不是Bomb类的对象的引用,而是TimerTask类的对象的引用,如果在内部类当中要显示引用外部类的this,要用外部类的类名.this,比如这是就要把uc.booms.remove(this);换成uc.booms.remove(Bomb.this);

cloudeecn@gmail.com

看不到完整代码,个人觉得原因在于,楼主的Timers类没有重写equals方法,remove(Object obj)方法是依赖于equals方法的:

以下文字出自java 1.6 API中文文档:

boolean remove(Object o)

从此列表中移除第一次出现的指定元素(如果存在)(可选操作)。如果列表不包含元素,则不更改列表。更确切地讲,移除满足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i 的元素(如果存在这样的元素)。如果此列表已包含指定元素(或者此列表由于调用而发生更改),则返回 true。

楼主没有为Timers重写equals函数,则在remove操作中调用的是默认的equals操作,即比较当前引用和待比较的引用是否相等..这个显然不等.因此找不到在booms中存在的与this相等的元素,因此,移除失败..

liushebiao的回答,恩恩,不过就算重写了equals方法也还是没有用的
你看看你在哪里调的remove(this) 这里的this貌似指代的是new TimerTask()对象吧,
怎么样也不可能移除boom吧

请把问题描述出来。

你可以自己回答这个问题,把剩下的部分写答案中啊。

同志,你想干吗