aspose file tools*
The moose likes Beginning Java and the fly likes Initializing and adding to an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing and adding to an ArrayList" Watch "Initializing and adding to an ArrayList" New topic
Author

Initializing and adding to an ArrayList

Timothy Stone
Ranch Hand

Joined: Aug 01, 2001
Posts: 71
... 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?


Timothy Stone, MIT, SCJP
http://www.petmystone.com/
"This Satan's drink [coffee] is so delicious, we shall cheat Satan and baptize it." --Pope Clement the VIII (1592-1605)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Aug 01, 2001
Posts: 71
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
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializing and adding to an ArrayList