我试图从ArrayList中删除一个元素,但什么也没有发生。
这是一个Android开发课程--必须通过索引从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.
我希望输出的是带有# bigPos元素的注释ArrayList已经消失。不会发生任何变化。
发布于 2019-06-10 07:43:37
第1步:对bigPos
使用int
,而不是Integer
第2步:从remove()
调用中删除new Integer()
现在,我认为您正在尝试删除一个其值是bigPos
的字符串表示的String
。remove()
有两个变体:
remove(int)
removes by indexremove(Object)
removes by value如果出于某种原因,您确实想在bigPos
中使用Integer
,那么请将remove(new Integer(bigPos))
替换为remove(bigPos.intValue())
。
https://stackoverflow.com/questions/56519137
复制相似问题