1

if I create a new arraylist of size 5...

    Player P;
    ArrayList<Player> orderedPlayers = new ArrayList<Player>(5);

and then I try to add to the middle of that arraylist...

    orderedPlayers.add(2, P);

I get an indexoutofbounds... I also get indexoutofbounds if I use set instead of add...

    orderedPlayers.set(2, P);

in fact the only way I can add P to the arraylist is if I use the 0 index...

    orderedPlayers.add(0, P);

and also for some strange reason when I do that my debugger in eclipse sees that element added to the 4th index of orderedPlayers instead of the 0th... is ArrayList buggy or am I completely missing something? how would I add to the middle of a null ArrayList?

matt
  • 83
  • 1
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/8896758/initial-size-for-the-arraylist – Raedwald Feb 08 '16 at 15:29
  • Possible duplicate of [ArrayList initial capacity and IndexOutOfBoundsException](http://stackoverflow.com/questions/11908037/arraylist-initial-capacity-and-indexoutofboundsexception) – Raedwald Feb 08 '16 at 15:31

7 Answers7

4

The 5 is the initial capacity, not the actual size. You can't add to the middle of an empty array list.

µBio
  • 10,371
  • 6
  • 36
  • 55
2

Here is probably what you want to do in order to initialize orderedPlayers:

ArrayList<Player> orderedPlayers =
  new ArrayList<Player>(Collections.nCopies( 5, null ));

Then you have a list with 5 null elements, at this point you can insert in the middle of it.

Alexander Pogrebnyak
  • 42,921
  • 9
  • 97
  • 117
  • This seems like trying to shoehorn a list to do something it wasn't made to do. If you really, really want an array, you should use an array. I feel like there's a more idiomatic use of `List` that could work for whatever the asker is attempting to do. – ColinD Aug 26 '10 at 20:00
1

When you create an ArrayList with a number in parameter, this number will be used to set the initial capacity. That way the List won't need to create a new array for "every" call to add(). If you can tell that your List will contain 100 elements, you can use new ArrayList(100). That doesn't mean that the 100 elements exists or are accessible.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

Colin Hebert
  • 85,401
  • 13
  • 150
  • 145
0

ArrayLists are dynamic structures... if you want to be able to add stuff at specific locations, you might have to use an array instead.

npinti
  • 50,175
  • 5
  • 67
  • 92
0

you may initialize your array list by NULL objects in a loop.

or even if the size is fixed. then use Array instead of List.

mhshams
  • 13,830
  • 15
  • 49
  • 63
0

You seem to be under the mistaken impression that when you write new ArrayList<Player>(5), you're getting something similar to a 5-element array. You aren't. You're getting an empty list that happens to be backed by an internal array that can initially hold up to 5 elements. The list itself, however, has no elements at all.

Lists are dynamic and grow and shrink as elements are added to them and removed from them. You start out with 0 elements. If you then add 5 players, the list will have 5 elements. If you just use the normal add(T) method with no index, you'll end up with the elements in the order they were added.

ColinD
  • 103,631
  • 27
  • 195
  • 199
0

By constructing an ArrayList with an initial capacity ArrayList(int initialCapacity), you are indicating roughly how large the list can get before Java will increase its size.

You are not allocating slots, as in an array. The ArrayList's size is based on the number of elements in the list, and you cannot insert or set a value to an index greater than its size.

Matthew Flynn
  • 2,162
  • 18
  • 26