简体   繁体   English

插入java.util.List中的任何位置

[英]Insert at any position in java.util.List

According to the docs you can insert objects an any position in a List: 根据文档,您可以在List中的任何位置插入对象:

The user of this interface has precise control over where in the list each element is inserted. 该接口的用户可以精确控制列表中每个元素的插入位置。

(source: http://download.oracle.com/javase/6/docs/api/java/util/List.html ) (来源: http//download.oracle.com/javase/6/docs/api/java/util/List.html

But the following program fails with an IndexOutOfBoundsException: 但是以下程序因IndexOutOfBoundsException而失败:

import java.util.ArrayList;

public class Test {
    public static void main(String[] args) {
        ArrayList<String> myList = new ArrayList<String>();
        myList.add(0, "derp");
        myList.add(2, "herp");

        for (String s : myList) {
            System.out.println("Le string: " + s);
        }
    }
}

It doesn't help setting initial capacity explicitly, either (which makes some sense since the default value is 10). 它也没有明确地设置初始容量(这是有道理的,因为默认值是10)。

Why can't I insert objects at any position as long as its index is lower than the capacity? 为什么我不能在任何位置插入对象,只要它的索引低于容量? Is the size always equal to the number of inserted elements? 尺寸是否始终等于插入元素的数量?

You can insert an object at any valid position. 您可以在任何有效位置插入对象。 Take a close look at the Javadoc for add(int, E) : 仔细看看Javadoc的add(int, E)

Throws: 抛出:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size()) IndexOutOfBoundsException - 如果索引超出范围(index < 0 || index > size())

In other words, inserting an element always increases the size of the list by 1. You can insert at either end, or in the middle... but you can't insert past the end. 换句话说,在插入的元素总是增加名单由1的大小可以在任意一端插入,或者在中间......但你不能插入过去的结束。

The capacity of an ArrayList is effectively an implementation detail - it controls when the backing array needs to be replaced by a larger one to cope with more elements. ArrayList容量实际上是一个实现细节 - 它控制何时需要用更大的替换替换阵列以应对更多元素。 The size of a list is the important part here - a list with capacity 100 but size 5 is still only a list of 5 elements, and so inserting at position 67 into such a list would make no sense. 列表的大小在这里是重要的部分 - 容量为100但是大小为5的列表仍然只是5个元素的列表,因此将位置67插入到这样的列表中是没有意义的。

List capacity is not the same as its size. 列表容量与其大小不同。

The capacity is a property of array backed lists (such ArrayList or Vector ), and it is the allocated size of the backing array (that is, the maximum number of items that you could put before needing to grow the structure). 容量是数组支持列表(例如ArrayListVector )的属性,它是支持数组的已分配大小(即,在需要增长结构之前可以放置的最大项目数)。

The size, as you say, is the number of elements present in the list. 正如您所说,大小是列表中存在的元素数量。

Then, why wouldn't you be able to insert an element wherever you want as long as there is space for it? 那么,只要有空间,为什么你不能在任何地方插入元素? Simple, because the List interface does not specify how the object is backed, and you couldn't do it in something like a LinkedList ; 很简单,因为List接口没有指定对象的备份方式,而且你不能像LinkedList这样做; so the homogeneous (and correct) behaviour is to throw an exception when that happens. 因此,同类(和正确)行为是在发生这种情况时抛出异常。

So you have two options: 所以你有两个选择:

  • Initialize the list properly by adding a default values up to your desired size. 通过添加最大所需大小的默认值来正确初始化列表。
  • If null is a sensible default value for you, you can use an array directly. 如果null是一个合理的默认值,则可以直接使用数组。

myList.add(2, "herp") should be myList.add(1, "herp") myList.add(2,“herp”)应该是myList.add(1,“herp”)

As increases the size of List is by 1,not 2. 随着List的大小增加1而不是2。

ArrayList has two members: capacity and size ArrayList有两个成员:容量和大小

capacity is the length of the underlying array, size is the length of the array the ArrayList represents capacity是底层数组的长度,size是ArrayList表示的数组的长度

so you have to add data in the list, so that the ArrayList itself gains the size where you want to insert data 所以你必须在列表中添加数据,以便ArrayList本身获得你想要插入数据的大小

The size of the list is Always equal to the number of the inserted elements 列表的大小始终等于插入元素的数量

Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

javadoc 的javadoc

First myList.add(0, "herp") will be inserted then it will check for size. 首先myList.add(0, "herp")然后检查大小。 Then, size is 1 but you are inserting at position 2 . 然后,大小为1但您插入位置2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM