在数组列表中删除和重新排列 [英] Remove and rearrange in arraylist

查看:29
本文介绍了在数组列表中删除和重新排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList 并且我打算在 android 中使用 java.util.ArrayList.remove() 方法,但我的问题是你是否有一个 ArrayList 具有以下索引的值,然后在索引处删除,其他索引是否会被撞到索引?这里有一种更简单的方法来理解我要问的问题:

<前>启动数组列表:------------------------|索引 |价值 |------------------------|0 |"一个" ||1 |"b" ||2 |"c" ||3 |"d" ||4 |"e" |------------------------

然后我在索引 3 处删除新数组是数组 A 还是数组 B

<前>数组列表 A: 数组列表 B:------------------------ ------------------------|索引 |价值 ||索引 |价值 |------------------------ ------------------------|0 |"一个" ||0 |"一个" ||1 |"b" ||1 |"b" ||2 |"c" ||2 |"c" ||3 |||3 |"e" ||4 |"e" |------------------------------------------------

解决方案

会是B,如果使用索引操作修改一个arraylist,ArrayList会重新排列为你已经明白了.这就是为什么 ArrayLists 如果有太多的修改,成本会很高

这就是 remove(int) 文档说

<块引用>

删除此列表中指定位置的元素.任意移动左边的后续元素(从它们的索引中减去一).

I have an ArrayList and I was going to use the java.util.ArrayList.remove() method in android but my question is if you have an ArrayList 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 why ArrayLists are costly if there are too many modifications

This is what remove(int) documentation says

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

这篇关于在数组列表中删除和重新排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆