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

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

问题描述

我有一个的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)文件说,


  

移除此列表中指定位置的元素。任何转移
  后续元素向左(减去一个来自其索引)。


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天全站免登陆