trinityalps
trinityalps

Reputation: 397

ArrayList data structure index out of bounds

I am trying to create an array list with an initial size of 10 using this code.

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. I just don't understand how to do this. Also startMessages is declared in the method as a List<EWSMessages>.

Upvotes: 0

Views: 82

Answers (3)

hyperpallium
hyperpallium

Reputation: 589

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.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86391

This line creates a list with an initial capacity of 10 -- but size zero.

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:

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));
    }
}

Upvotes: 2

RealSkeptic
RealSkeptic

Reputation: 34618

An ArrayList is not an array. 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.

But an ArrayList is not like that. 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. 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. 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.

If you need to reverse a list, you can use 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.

Upvotes: 1

Related Questions