This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

Whizlab question on Vector

Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
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

Joined: Sep 08, 2003
Posts: 116
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?

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Pierre Henry
Ranch Hand

Joined: Oct 01, 2003
Posts: 31
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.

"A good method shouldn't bypass one screen long"<br />"genius is the knowledge left when one has forgotten everything"
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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

Joined: Nov 09, 2003
Posts: 65
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

I agree. Here's the link: http://aspose.com/file-tools

subject: Whizlab question on Vector