1
import java.util.ArrayList;
public class WTFAMIDOINGWRONG 
{
    public static void main(String[] args)
    {
        ArrayList<Integer> intsAR = new ArrayList<Integer>(5);
        intsAR.add(3, 1);

    }
}

So, I've been fooling around with this for about an hour and I haven't the slightest Idea what I could be doing wrong. No matter what I do, it's convinced the arraylist has no size and everything is therefore out of bounds. If anyone could tell me what I'm doing wrong I'd really appreciate it.

3
  • Why don't you simply use instAR.add(1);? It will not require any index. Commented Feb 28, 2011 at 4:41
  • Ok, the issue here is that I'm trying to satisfy a requirement for a programming course I'm taking. It says I must add a new item to the middle and end of the list. So I can't simply use intsAR.add(1);. Commented Feb 28, 2011 at 4:42
  • Then use array, int[], instead. Commented Feb 28, 2011 at 4:48

3 Answers 3

4

An ArrayList is backed by an array, so when you specify the initial capacity, you are specifying how large of an array to allocate. This is important because it specifies how much memory the ArrayList will occupy sequentially.

However, the size of the ArrayList specifies how many items are actually in the list. Once the list reaches a certain size (relative to the capacity of the backing array), the backing array will be reallocated to take up additional space.

If you wanted to create an ArrayList of 10 items, all with 0, you would do:

List<Integer> list = new ArrayList<Integer>();
for ( int i = 0; i < 10; i++ ) {
   list.add(0);
}

Now you could insert an item at position 3 (or somewhere in the middle) if you wanted to.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't get it to work. I tried i < list.size() earlier, but that just brings me back to why it's not working in the first place,ha.
Sure. You might also want to look at the source code for ArrayList to get a better understanding of how it works.
A shorter way to write this: List<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, 0)); or List<Integer> list = new ArrayList<Integer>(); list.addAll(Collections.nCopies(10, 0));.
0

Because the size of your list is ZERO. Yes, you are actually constructing it by specifying the initialCapacity, but that doesn't mean size. Are you getting my point? You can say that taht will just reserve the space for future.

BTW, size() documentation clearly states that, it is the number of elements in the list. Now, I hope you know what is happening.

Comments

0

You cannot insert into an empty list in position 3 - what would be the first 2 elements then? With empty list only intsAR.add(0, 1); will work

1 Comment

is it possible then, to just set a default value for every item in the list? I tried using a loop to add a value to everything and I just received the same out of bounds error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.