Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing and adding to an ArrayList

 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... so this is an odd question, but the documentation is a bit muddy on the actual execution of adding to an ArrayList (I should know this...)



So, now I have a nice IOOBE. The reason, apparently, is that in the loop a comparison is performed that assumes incoming objects are "ordered," e.g., 1, 2, 3..., but may come as: 2, 1, 3...

Hence, Object 2 belongs in the second index of the ArrayList (index 1).

The IOOBE seems to stem from this as the ArrayList size is initially zero (0) and there is no position 1. Initial capacity and ensuring of the capacity does not help.

Are these observations correct?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's an IOOBE? Please look at this FAQ.

Why are you adding at position 1 in the first place?

Look at the Java™ Tutorials under "list interface" and you see the details of how you add to a List. Note that your three elements are not numbered 1 2 3, but 0 1 2. You can only add to position i of a List under the circumstances described here. When you have an empty List, the only position available to add at is 0.

Sorry for posting an incomplete posting (which I shall delete) earlier.
 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You win for "rules lawyering."

IOOBE, not a real word, is easier than writing IndexOutOfBoundsException. Though, I could have cited first, then used the "acronym."

I know that an ArrayList is zero-indexed. I did pass the SCJP exam...allow me to bask in my self congratulatory sun...now...

What I was trying to say, though I was unclear, is better demo'd in code...



What I have found is that if the order of the products provided in the original list is not naturally ordered, and arrives at line 16 above, the ArrayList throws an IOOBE on the add method.

What is not clear in the ArrayList documentation is that the first item added to an ArrayList must be at index 0.

Or is that documented somewhere; everywhere I looked it seemed assumed (and is a safe assumption I suppose), but the developer writing the above code, of which I'm reviewing, seems to insist this is possible. I couldn't refute, so I came here.

Regards,
Tim
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Stone wrote:
What is not clear in the ArrayList documentation is that the first item added to an ArrayList must be at index 0.

Or is that documented somewhere; everywhere I looked it seemed assumed (and is a safe assumption I suppose), but the developer writing the above code, of which I'm reviewing, seems to insist this is possible. I couldn't refute, so I came here.


From the JavaDoc...

add
public void add(int index, E element) Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())


Seems pretty clear to me.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to use a List for ordering or sorting? Lists are not naturally sorted, but are naturally ordered. That means the elements are arranged from first added to last added, although as we have already seen you can alter the order when inserting elements. There are subtypes of List, viz queues which usually release the first-entered item, and Stacks which usually release the last-added item.
You can have Sets, which implicitly have neither order nor sorting; the elements in a set are not returned in a predictable order.
You can however have special kinds of set which are implicitly sorted. eg a TreeSet, but remember that sets implicitly delete duplicate entries.

What you appear to want is to enter all your values into a List retaining entry order . . . then sort them afterwards.

I am sorry, but I thought I had given you the link to the API which Henry quoted earlier tonight; I was mistaken and copied the wrong link. Sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic