• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vectors vs Arrays  RSS feed

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can u match something in a vector this is in an array? ...or put elements in a vector into an array?
[ February 16, 2006: Message edited by: Sam Bluesman ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first, you would have to write a routine to accomplish the task. Something simple like:



This is a worst case scenario, when the Vector may contain specific classes which you don't know all the data about, but you can produce a clone with comparable data.

As for your second, very simple: Object[] Vector.toArray()
 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. Could u give me an example of Object[] Vector.toArray() using variables?

cheers
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

Originally posted by Sam Bluesman:
Can u match something in a vector this is in an array?


Before you could do any meaningful comparisons between the contents of an Array & Vector, you need to know if they could be meaningfully/logically compared. For example, you can't be checking if an orange in a Vector is inside an Array full of apples. You've to compare an orange with an orange, i.e., compare objects of the same class (type).

To lighten your load, you could convert that Array into a Collection, and if the objects inside your Array & Vector implemented equals method, you could use the Collection method for comparing if a vector is contained in another. Check out the API for more information.

Originally posted by Sam Bluesman:
or put elements in a vector into an array?


Well, convert the Array into a Collection, then add it to the Vector, then convert it to Array again, if needed. Read the API for the exact how to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!