首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓ArrayList移除只是简单的不起作用

安卓ArrayList移除只是简单的不起作用
EN

Stack Overflow用户
提问于 2019-06-10 07:39:45
回答 1查看 2.8K关注 0票数 0

我试图从ArrayList中删除一个元素,但什么也没有发生。

这是一个Android开发课程--必须通过索引从ArrayList中删除一个元素。我在删除之前和之后放置了日志,但没有任何反应。

代码语言:javascript
复制
    // at the top of MainActivity.java

    ArrayList<String> notes;
    Integer bigPos;

    // in a dialog box positive button onClick method
    notes.remove(new Integer(bigPos)); // just to force it to execute the integer method
    // bigPos is set to pos in the long click listener before the alert.show is executed. I know from logs that the bigPos and notes are in scope. the code RUNS it just doesn't DO anything... notes is the same after the removal.

我希望输出的是带有# bigPos元素的注释ArrayList已经消失。不会发生任何变化。

EN

回答 1

Stack Overflow用户

发布于 2019-06-10 07:43:37

第1步:对bigPos使用int,而不是Integer

第2步:从remove()调用中删除new Integer()

现在,我认为您正在尝试删除一个其值是bigPos的字符串表示的Stringremove()有两个变体:

  • remove(int) removes by index
  • remove(Object) removes by value

如果出于某种原因,您确实想在bigPos中使用Integer,那么请将remove(new Integer(bigPos))替换为remove(bigPos.intValue())

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56519137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档