删除和数组列表中重新排列 [英] Remove and rearrange in arraylist
问题描述
我有一个的ArrayList
和我要使用 java.util.ArrayList.remove()
方法在android系统,但我的问题是,如果你有一个的ArrayList
里面有值在下面的索引,然后在索引中删除,将别人撞了一个指数?在这里,一个更简单的方法来了解我想问:
开始的ArrayList:
------------------------
|首页|值|
------------------------
| 0 | 一个|
| 1 | b的|
| 2 | C|
| 3 | D|
| 4 | E|
------------------------
然后我删除索引3将新数组是数组类型的A或排列B
ArrayList中答:ArrayList中乙:
------------------------ ------------------------
|首页|值| |首页|值|
------------------------ ------------------------
| 0 | 一个| | 0 | 一个|
| 1 | b的| | 1 | b的|
| 2 | C| | 2 | C|
| 3 | | | 3 | E|
| 4 | E| ------------------------
------------------------
这将是的乙后,如果您在使用索引操作修改一个ArrayList 的ArrayList
您已经明白将重新排列。这就是为什么的ArrayList
是,如果有太多昂贵的修改
这是什么 remove(int)$c$c>文件说,
移除此列表中指定位置的元素。任何转移
后续元素向左(减去一个来自其索引)。
块引用>I have an
ArrayList
and I was going to use thejava.util.ArrayList.remove()
method in android but my question is if you have anArrayList
which has the values at the following indexes, then remove at index, will the others be bumped up an index? Here an easier way to understand what I am trying to ask:Starting Arraylist: ------------------------ | Index | Value | ------------------------ | 0 | "a" | | 1 | "b" | | 2 | "c" | | 3 | "d" | | 4 | "e" | ------------------------Then I remove at index 3 will the new array be array A or array B
Arraylist A: Arraylist B: ------------------------ ------------------------ | Index | Value | | Index | Value | ------------------------ ------------------------ | 0 | "a" | | 0 | "a" | | 1 | "b" | | 1 | "b" | | 2 | "c" | | 2 | "c" | | 3 | | | 3 | "e" | | 4 | "e" | ------------------------ ------------------------
解决方案It will be B, If you modify an arraylist using index operations,
ArrayList
will be rearranged as you have understood. Thats whyArrayLists
are costly if there are too many modificationsThis is what
remove(int)
documentation saysRemoves the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
这篇关于删除和数组列表中重新排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!