【发布时间】:2014-01-25 18:00:27
【问题描述】:
我正在尝试创建一个方法,removeAnObject,它从数组中删除一个对象并返回一个布尔值,但是当我尝试从Arraylist 类。
方法如下:
public boolean removeAnObject(Element anObject)
{
int whereWeAre;
String paramClass;
String currClass;
boolean weFoundIt;
paramClass = anObject.getClassName();
whereWeAre = 0;
weFoundIt = false;
while(whereWeAre != currentSize && weFoundIt == false)
{
currClass = theList[whereWeAre].getClassName();
if(currClass.equals(paramClass))
{
theList.remove(whereWeAre);
weFoundIt = true;
}
else
{
whereWeAre++;
}
}
return weFoundIt;
}
这是错误:
ElementSet.java:262: error: cannot find symbol
theList.remove(theList[whereWeAre]);
^
symbol: method remove(Element)
location: variable theList of type Element[]
1 error
最后说明:我在课程的开头确实有import java.util.ArrayList。
【问题讨论】:
-
您不能在数组上调用
remove方法——只能调用Object方法。 -
数组没有删除方法。 ArrayList 是由数组支持的 List 的实现。我认为您可能会混淆术语 /ideas...
-
如果
theList的声明是Element[] theList,那么它是一个常规数组,而不是ArrayList。你不得不说这是一个ArrayList:ArrayList<Element> theList。 -
不应该是
return true而不是return weFoundIt吗?因为它已经是public boolean方法了。 -
@Xbit 如果
weFoundIt是false,他为什么要返回true?
标签: java inheritance methods compiler-errors