【问题标题】:I cannot use the remove(int) method, symbol not found error我不能使用 remove(int) 方法,找不到符号错误
【发布时间】: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 如果weFoundItfalse,他为什么要返回true

标签: java inheritance methods compiler-errors


【解决方案1】:

ArrayList 的参数为 int 的 remove(parameter) 方法也返回类型 E,即创建列表时指定的 Object 类型的 Object。

所以对于初学者来说,当你使用 theList[] 我假设它是一个 ArrayLists() 的数组 并且您确定 theList 的类型是 ArrayList 而不是 Array。

1- 保存并重新编译您的代码,错误行不在您指定的代码中。
2- 如果您使用上述指定的方法,请确保 theList[whereWeAre] 是一个 int。

3- 为什么不直接使用 ArrayList() 类中包含的这个方法:

public boolean remove(Object o)

这将删除对象的第一次出现,如果成功则返回true。 在此处了解更多信息:Docs

【讨论】:

  • 嗯,这样会更容易使用,谢谢你提供这些信息。
【解决方案2】:

如果使用数组,则替换:

theList.remove(whereWeAre);

theList[whereWeAre] = null;

此行将使whereWeAre 为空,从而删除搜索到的Element

如果您打算在同一个数组上多次重用您的方法,请不要忘记添加一个空检查条件。否则,您将面临NullPointerException

     while(whereWeAre != currentSize && weFoundIt == false)
     {
        if (theList[whereWeAre]!=null)
        {
            currClass = theList[whereWeAre].getClassName();
            if(currClass.equals(paramClass))
            {
               theList.remove(whereWeAre);
               weFoundIt = true;
            }
            else
            {
               whereWeAre++;
            }
        }
        else
        {
            whereWeAre++;
        }
     }

【讨论】:

  • @user3150298 这可能不是您想要的。这个解决方案将改变removeAnObject 所做的整个设计。效果将是留下一个带有孔的数组,并且该数组的大小与以前相同。如果theListArrayList 并且您使用了remove,那实际上会删除元素并缩短数组,并且被删除元素上方的元素将向下移动,现在将位于不同的索引处。您必须意识到差异并决定您想要的效果。
  • 我不赞成这个答案,因为在仔细观察之后,它会失败。如果删除元素,原始代码不会增加whereWeAre,如果这是ArrayList 并且它实际上挤出了元素,那将是正确的。但如果这是唯一的更改,将会发生的情况是 theList[whereWeAre] 将设置为 null,whereWeAre 不会递增,程序将循环返回,然后 theList[whereWeAre].getClassName() 将抛出 NPE。
  • @ajb 程序将退出循环,因为weFoundIt 为真。没错,如果在同一个数组上多次调用该方法,则必须更改为空检查。
  • 抱歉,我错过了——我以为他正在删除每个出现的对象。
  • @ajb 无论如何,您的 downvoting 揭示了一个错误,以防在同一个数组上多次使用相同的方法:p。但是 OP 没有给我们他的背景......家庭作业或生产代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 2018-01-23
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多