简体   繁体   English

ArrayList数据结构索引超出范围

[英]ArrayList data structure index out of bounds

I am trying to create an array list with an initial size of 10 using this code. 我正在尝试使用此代码创建一个初始大小为10的数组列表。

List<EWSMessage> messages = new ArrayList<EWSMessage>(10);
List<EWSMessage> newMessages =  new ArrayList<EWSMessage>(startMessages.size() - 10);

for (int i = 0, count = startMessages.size(); i < count; i++) {
  if (i < 10) {
    messages.set(i, startMessages.get(i));
  } else {
    newMessages.set(i - 10,startMessages.get(i));
  }
}

My issue is I need to fill the array list before I can set the values in the for loop. 我的问题是,我需要先填充数组列表,然后才能在for循环中设置值。 I just don't understand how to do this. 我只是不知道该怎么做。 Also startMessages is declared in the method as a List<EWSMessages> . 同样, startMessages在方法中声明为List<EWSMessages>

This line creates a list with an initial capacity of 10 -- but size zero. 该行创建一个初始容量为10的列表,但大小为零。

List<EWSMessage> messages = new ArrayList<EWSMessage>(10);

Providing the initial capacity is optional. 提供初始容量是可选的。 The list will grow as you add elements to it. 当您向其中添加元素时,该列表将会增长。

An easy way to add elements is with the add() method : 添加元素的一种简单方法是使用add()方法

List<EWSMessage> messages = new ArrayList<EWSMessage>();
List<EWSMessage> newMessages =  new ArrayList<EWSMessage>();

for (int i = 0, count = startMessages.size(); i < count; i++) {
    if (i < 10) {
        messages.add( startMessages.get(i));
    } else {
        newMessages.add( startMessages.get(i));
    }
}

An ArrayList is not an array. ArrayList不是数组。 In an array, when you create it, you say how many places you need, and then you can access all of them. 在数组中,创建数组时,说出所需的位置,然后可以访问所有这些位置。 If you said: 如果你说:

int[] arr = new int[10];

Then you can use arr[5] and arr[8] etc. Immediately. 然后,您可以立即使用arr[5]arr[8]等。

But an ArrayList is not like that. 但是ArrayList不是那样的。 It only has as many items as you add to it. 它仅包含您添加的项目数。 The number that you pass to the constructor just tells it how many places you think you may need, but it's not the number of items available in the list - the initial size is 0. After you add an item, the size is 1, and so on. 传递给构造函数的数字只是告诉它您认为可能需要多少个位置,但不是列表中可用项目的数量-初始大小为0。 add项目后,大小为1,然后以此类推。 So you don't actually have to pass a number when you create the list: 因此,您在创建列表时实际上不必传递数字:

List a = new ArrayList();  // The size is 0 but you can add items.
List b = new ArrayList(10); // The size is also 0! The 10 is just the capacity!

The set method changes an existing item. set方法更改现有项目。 If you didn't add an item, then it won't work - it complains that you are trying to change an item that doesn't exist. 如果您没有添加项目,那么它将无法正常工作-它抱怨您正在尝试更改不存在的项目。 So first, you need to use add to add items to the list, and then you can change them with set . 因此,首先,您需要使用add将项目add到列表中,然后可以使用set更改它们。

If you need to reverse a list, you can use Collections.reverse(list) . 如果需要反转列表,则可以使用Collections.reverse(list) It will reverse the given list. 它将反转给定的列表。 If you want to have one straight list and one reverse list, you can do: 如果要有一个直接列表和一个反向列表,则可以执行以下操作:

// Fill list 1 by adding to it, then:

List list2 = new ArrayList(list1);
Collections.reverse(list2);

This will first copy all the items from list1 to list2 , then reverse list2 . 这首先将所有项目复制从list1list2 ,然后反转list2

BTW a different approach, that creates views backed by the original list. 顺便说一句,另一种方法是创建由原始列表支持的视图。

int size = startMessages.size();
List<EWSMessage> messages = startMessages.subList(0, Math.min(10, size));
List<EWSMessage> newMessages =  startMessages.subList(10, Math.max(10, size));

I would claim this was clearer, but for the min/max checks for fewer than 10. 我会说这更清楚,但是对于最小/最大检查少于10个。

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

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