gpt4 book ai didi

java - Android ArrayList 删除只是简单不起作用

转载 作者:行者123 更新时间:2023-12-02 01:28:17 25 4
gpt4 key购买 nike

我试图从 ArrayList 中删除一个元素,但没有任何反应。

这是一个 Android 开发类(class) - 必须按索引从 ArrayList 中删除一个元素。我在删除之前和之后都放置了日志,但没有任何反应。


// 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.

我希望输出是注释 ArrayList,其中元素 # bigPos 消失。没有发生任何变化。

最佳答案

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

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

就目前情况而言,我认为您正在尝试删除一个 String,其值是 bigPos 的字符串表示形式。 remove() 有两种变体:

  • remove(int) 按索引删除
  • remove(Object) 按值删除

如果由于某种原因您真的非常想将 Integer 用于 bigPos,则将 remove(new Integer(bigPos)) 替换为删除(bigPos.intValue())

关于java - Android ArrayList 删除只是简单不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56519137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com