• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Arraylist Question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick question that someone might be able to answer for me...
If the default size of an arraylist is 10, why does the following code print an IndexOutOfBoundsException?
I would have thought it should print Null? or at least throw a NullPointerException?

al.size returns 2. Therefore the 3rd element in the arraylist. The 3rd element should be Null, no?

I can understand how an IndexOutOfBoundsException would be thrown if al.size returns 10 (the default size) as you would be trying to access the 11th element in the arraylist.

I am right in saying then that the default size is 10 but at runtime the arraylist size will be set to the current number of element if the number is less then the default size...?

 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Obrien wrote:
If the default size of an arraylist is 10, why does the following code print an IndexOutOfBoundsException?
I would have thought it should print Null? or at least throw a NullPointerException?



No. The default capacity is 10. A "capacity" is an implementation detail -- it is the size of the internal array used to hold the elements, and when the size exceeds the capacity, the collection will automatically allocate a larger internal larger array and copy the elements over. The users of the collection should not care about this detail.

As for why it throws an IndexOutOfBoundsException instead of a NullPointerException -- it is clearly defined in the javadoc. See java.util.ArrayList.

Henry
 
Henry Wong
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Obrien wrote:
al.size returns 2. Therefore the 3rd element in the arraylist. The 3rd element should be Null, no?




The size() method returns the number of elements -- and since the list is zero indexed, you have valid elements at position zero and one.


As for whether the 3rd element of the internal array is null or not, it is not relevant. The behavior of the set() method is defined by the JavaDoc.

Henry
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The arraylist index starts from 0. Since you are adding 2 items to the list, it sits in al[0] and al[1]. But while performing al.get(), you are passing the arraylist size (which is 2 in this case) and hence its throwing IndexOutOfBoundsException.

 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies guys.

No. The default capacity is 10. A "capacity" is an implementation detail -- it is the size of the internal array used to hold the elements, and when the size exceeds the capacity, the collection will automatically allocate a larger internal larger array and copy the elements over. The users of the collection should not care about this detail.



This is where my question stems from. Since the default capacity/size is 10... Why does Size() not return the actual size of the arraylist at Runtime? - Is this not 10?
Initialised with enties at Index 0 and 1 of "111" and "222" - and index 2 to 9 containing a default value of Null.

I have read the entry in the JavaDoc for arraylists and it simply states - IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
If the size is 10 both sides of this statement are false.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jake Obrien wrote:If the default size of an arraylist is 10, ...


The default size is 0, not 10.

Jake Obrien wrote:This is where my question stems from. Since the default capacity/size is 10... Why does Size() not return the actual size of the arraylist at Runtime? - Is this not 10?


No, you're confusing "capacity" and "size". The default capacity is 10, but the default size is 0. The size is the actual number of elements that you've added to the ArrayList.

You're better off forgetting about "capacity", because it has to do only with the internal workings of ArrayList, and you don't need to know anything about that to be able to use ArrayList.

If you want to know the details: Internally, ArrayList stores its elements in an array. As you know, arrays have a fixed size in Java - once you've created an array, you cannot change its size. When you add elements to an ArrayList, then it stores those elements in its internal array. If you add more elements, then at some point the internal array is going to be too small. The ArrayList then creates a new, larger, internal array, and copies the content of the old array to the new array. The capacity of an ArrayList is the actual length of its internal array. For efficiency, an ArrayList doesn't start with an internal array of length 0; instead, it starts with an array of length 10, so that it doesn't have to re-allocate the array for the first 10 times that you add an element. And when you add more elements, it doesn't grow the array by just one element, but it adds a bunch, so that it can avoid re-allocating the array for each element that you add.

size = the actual number of elements in the ArrayList
capacity = the number of elements that can be stored in the internal array before the array needs to be re-allocated
 
Jake Obrien
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Explanations much appreciated.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic