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

Whizlab question on Vector

 
Mohit Goyal
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Vector maintains object refrences in the order they were added.
ans given is true.
According to me ans should be false because the Vector is ordered by index not by insertion order
 
Vishy Karl
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

According to me ans should be false because the Vector is ordered by index not by insertion order

Isn't the insertion order same as ordered by the index ??
Do clarify?
 
Pierre Henry
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit,
The method

adds o to the end of the vector.
The method

inserts element at position index, 0<=index<size()
This means that when you add an object to a vector , this is added at
the end, so it maintains the order.
 
Vad Fogel
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohit Goyal:
A Vector maintains object refrences in the order they were added.
ans given is true.
According to me ans should be false because the Vector is ordered by index not by insertion order

The wording sounds a bit ambiguous to me. Well, let's see how a Vector maintains order of its elements. There're 3 methods that put a new element at the end of the vector so that the resulting order is in fact the insertion order:

And there're 2 other methods that allow you to insert element(s) at the specified position:

In both cases, the elements are accessible either through their indexes or the iterator() method. If the elements were added using the first group of methods, iterator() returns them in the order they were inserted.
 
Mohit Goyal
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Vad u really cleared my doubt. i was going through Khalid's book this evening in which it was given that Vector is ordered by insertion order. But I think u r right they ordered by both
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic