问题描述
方法何时以及为什么 boolean java.util.List.remove(Object object)
返回false?
When and why does the method boolean java.util.List.remove(Object object)
return false?
文档说明
为什么操作不会对列表生效?
Why wouldn't the operation take effect on the List?
(注意:我正在使用的List的实现是 ArrayList
)
(NB: The implementation of the List I am making use of is the ArrayList
)
更新:
我要删除的对象确实存在于列表中。我将该类添加到 List
(因此参数 this
)并传递此
(在同一个类中)到remove方法。因此,对象应存在于列表中(或不存在?)。
Update:The objects I am trying to remove do exist in the List. I add the class to the List
(hence the argument this
) and pass this
(within the same class) to the remove method. Therefor the object should exist in the List (or not?).
更新:
这是代码。看看 delay()
方法。我试图从列表中删除相应的类
。
public class Timer extends Object {
private static List<Timer> allTimers;
private Long startTime;
private int delayTime;
private boolean registered;
String name;
public Timer(String name) {
this.name = name;
this.registered = true;
allTimers.add(this);
Log.d("SpaceDroid", "Foo: Created timer: " + name + " Size: " + this.allTimers.size());
}
public Timer(String name, int delayTime) {
this(name);
this.delayTime = delayTime;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public boolean delay() {
if(this.startTime == null) {
this.startTime = System.currentTimeMillis();
}
if(startTime + this.delayTime < System.currentTimeMillis()) {
if(this.registered) {
Log.d("SpaceDroid", "Foo: Trying to remove timer: " + name);
if(this.allTimers.remove(this)) {
Log.d("SpaceDroid", "Foo: Successfully removed timer: " + name);
}
else {
Log.d("SpaceDroid", "Foo: Did not remove timer: " + name);
}
}
return true;
}
else {
return false;
}
}
// The same as resume, just renew the startTime
public void reset() {
this.startTime = null;
this.registered = true;
this.allTimers.add(this);
}
public void resume() {
this.startTime = System.currentTimeMillis();
}
public void pause() {
// Check if timer is still running
if(!this.delay()) {
// Calculate new delayTime
delayTime = (int) ((startTime + this.delayTime) - System.currentTimeMillis());
Log.d("SpaceDroid", "Foo: New delay time: " + delayTime + " for timer: " + name);
}
}
public static void resumeAllTimers() {
List<Timer> timers = new ArrayList<Timer>(Timer.allTimers);
for (Timer timer : timers) {
timer.resume();
}
}
public static void pauseAllTimers() {
List<Timer> timers = new ArrayList<Timer>(Timer.allTimers);
for (Timer timer : timers) {
timer.pause();
}
}
public static void disposeAllTimers() {
Timer.allTimers.clear();
Timer.allTimers = null;
}
public static void initializeAllTimers() {
allTimers = new ArrayList<Timer>();
}
}
推荐答案
如果传入的对象
实际上不在列表中,则不会对列表产生任何影响,因此返回false 。
If the Object
passed in is not actually in the list, it wouldn't take any effect on the list and therefore return false.
编辑(感谢Jai):
该方法使用特定的对象
s equals()
确定它是否包含该对象的方法。因此,如果您有自定义对象,请确保覆盖方法,以及(维护方法之间的一般契约)。
Edit (thanks Jai):The method uses the specific Object
s equals()
method to determine if it contains that object. So, if you have custom objects make sure you override the equals()
method, and also the hashCode()
(to maintain the general contract between the methods).
这篇关于何时以及为什么List remove(Object object)返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!