Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ArrayList<Double> IndexOutOfBoundsException Problem

I've got a Problem with ArrayList. I need it to store a result. Because I want to start with element n I tried to give the ArrayList a capacity with ensureCapacity(n+1) to use set(n,x) but I get an IndexOutOfBoundsException.

I tried to store n add(x) before the use of set and this works.

So I'd like to know why it doesn't work on my way and how to solve this because put n times a add(x) isn't a good style ;-)

like image 639
Sebastian Bechtel Avatar asked May 09 '10 18:05

Sebastian Bechtel


People also ask

Can a double value be stored in an ArrayList double >?

No, you cannot store doubles in ArrayList<Integer> without loss of precision. You can, however, store them in ArrayList<Double> .

Can you add the same object twice to an ArrayList?

If you try to add the same object twice, it will work, or if you try to add 2 objects with everything the same, it will still work. It is not best practice to do that because its harder to maintain the list.

Can ArrayList be double in Java?

ArrayList list = new ArrayList<double>(1.38, 2.56, 4.3); The first code showed that the constructor ArrayList<Double>(double, double, double) is undefined and the second code shows that dimensions are required after double .

Can ArrayList store doubles?

ArrayList cannot hold primitive data types such as int, double, char, and long.


3 Answers

When you change the capacity of an ArrayList it doesn't create any elements, it just reserves memory where there could be elements. You can check the size before and after adjusting the capacity and you will see that it does not change.

The purpose of changing the capacity is if you know in advance how many elements you will have, then you can avoid unnecessary repeated resizing as you add new elements, and you can avoid memory wastage from excess unused capacity.

like image 108
Mark Byers Avatar answered Oct 29 '22 07:10

Mark Byers


If you don't like using your own loop and the list add method directly then there is another way. Create your ArrayList with the number of elements you want it directly like this:

final int MAX_ELEMENTS = 1000;
List<Integer> myList = new ArrayList<Integer>(
    Collections.<Integer>nCopies(MAX_ELEMENTS, null));

Or, if you already have a list that you want to expand the size by n elements:

myList.addAll(Collections.<Integer>nCopies(n, null));

(Note, I assumed here that the list would be holding Integer objects, but you can change this to your custom type. If you are working with raw/pre-Java 5 types then just drop the generic declarations.)

As for your actual question: capacity != contents. An ArrayList internally has both a physical array and a count of what is actually in it. Increasing the capacity, changes the internal array so it can hold that many elements, however, the count does not change. You need to add elements to increase that count.

On the other hand, if you are just trying to set specific elements and know the maximum that you want to use, why not use an array directly? If you then need to pass this array to an API that takes Lists, then use Arrays.asList. The other classes could still change contents of your backing array but it would not be able to increase the size or capacity of it.

like image 43
Kevin Brock Avatar answered Oct 29 '22 06:10

Kevin Brock


As others have answered, ensureCapacity() is just related to performance, is not frequently used by the common user.

From Bruce Eckel's Thinking in Java book:

In a private message, Joshua Bloch wrote: "... I believe that we erred by allowing implementation details (such as hash table size and load factor) into our APIs. The client should perhaps tell us the maximum expected size of a collection, and we should take it from there. Clients can easily do more harm than good by choosing values for these parameters. As an extreme example, consider Vector's capacityIncrement. No one should ever set this, and we shouldn't have provided it. If you set it to any non-zero value, the asymptotic cost of a sequence of appends goes from linear to quadratic. In other words, it destroys your performance. Over time, we're beginning to wise up about this sort of thing. If you look at IdentityHashMap, you'll see that it has no low-level tuning parameters"

like image 29
leonbloy Avatar answered Oct 29 '22 07:10

leonbloy