0

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>.

x80486
  • 5,131
  • 4
  • 32
  • 71
trinityalps
  • 467
  • 4
  • 17
  • You now that an array list like an other list in java has no limit? Ok the heap size ;) But thats fair away from 10. Also when you define the arraylist with capacity of 10, it can grow aboth it! Don't mix ArrayList with Arrays. – Rene M. Aug 13 '15 at 20:10
  • Possible duplicate of [ArrayList initial capacity and IndexOutOfBoundsException](http://stackoverflow.com/questions/11908037/arraylist-initial-capacity-and-indexoutofboundsexception) – Raedwald Feb 08 '16 at 15:34

3 Answers3

2

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));
    }
}
Andy Thomas
  • 78,842
  • 10
  • 93
  • 142
1

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.

RealSkeptic
  • 32,074
  • 7
  • 48
  • 75
0

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.

hyperpallium
  • 481
  • 5
  • 16