This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about inserting elements into a treemap as compared to arraylist?

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could someone tell me why I can insert elements into a treemap in an unordered way say treemap.put(3,"Hello") for element 3, and then treemap.put(1,"hello2") for element 1, without there being any problems as compared to an arraylist, when I try the arraylist.add(3,""hello) at start, it tells me the arraylist is empty and cannot be inserted, but I haven't this issue with treemap. I even tried setting initial capacity of arraylist but it still told me the arraylist was empty. Just curious as to why.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact text of the Exception you are getting?

From the ArrayList source code you should only get a IndexOutOfBoundsException if the index is <0 or > size.

Bill
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you comparing the functionality of the TreeMap put method and the ArrayList add method ? They do different things. The first parameter to the put method is not an index to indicate the position you want the object in the map, it is a key that is used to access an associated value in the map. The two calls only look alike becaus eyou used an integer as the key. If you do something like
it is more obvious that the two methods are totally unrelated.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47260
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read about that add() method?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume you have read about that add() method?
Me or Mark ?
 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers, thanks for replys. Thanks William, it was IndexOutOfBoundsException exception I was thinking. My fault, I was too restrictive in my thinking about comparing the 2 because I've been using integers as keys for the treemap. I have a much better understanding of how it works now. Just find it interesting though, that even if I allocate a predefined size for the arrayList, that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47260
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Campbell Ritchie wrote:I presume you have read about that add() method?
Me or Mark ?
Mark.

I know you have read it, Joanne.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47260
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark O' Sullivan wrote:. . . even if I allocate a predefined size for the arrayList, that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.
I think you have missed out a "not" somewhere, probably after "could".

You cannot allocate a size for a List, but a capacity. Fred Rosenberger talks about egg-boxes, so I shall use the same analogy. Writing ... = new ArrayList<Egg>(25); creates a reference to something representing 25 egg-boxes, each large enough to hold one egg, but all empty. So the collection can hold (capacity) 25 eggs, but does hold (size) none. Writing eggs.add(3, myEgg); adds an Egg just beyond the 3rd egg, and moves all the other eggs along to make space for it (if necessary). If you only have two Eggs already, where should you add it? In the 3rd box or the 4th? You could design a List which adds that Egg in the 3rd space, but ArrayList was designed differently, to throw a tantrum Exception, in those circumstances.
Note there are some sorts of List (eg LinkedList) whose capacity and size are always exactly the same.

Bertrand Meyer in Object-Oriented Software Construction says that all procedures to add something to a collection should have the same name. But I think I disagree with him. That is why there are different names for adding: in a List or Set “add”, in a Queue “offer”, in a Stack “push”, and in a Map “put”. The different names are supposed to remind you they do different things. If you look in the Deque (pronounced Deck, a portmanteau word from Double-Ended QUeuE) interface, there is a list of method names for several different kinds of collection.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark O' Sullivan wrote:
that it doesn't create predefined empty objects in memory, so I could do arraylist.add(3,""hello)? Thanks.

......... what is with the double quote above - ""hello ??

Looking at the source code, we see at ArrayList uses an array of Object references:



The default constructor uses a size of 10 for this array. These are "empty" references, ie null, not "empty objects."

You should therefore be able to place a reference at index 3 using add



AHA - further digging - Looks like size is initially zero so that add will only work for lists that already have at least index references

Bill


 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone for all their help on this topic. I'll sure someone in the same situation will find the question useful when they hit the same problem. Learned loads.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic