• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector Question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I have 2 vectors, vector1 has objects of person and vector2 has objects of company.

A new Vector should be created merging these 2 vectors. The object in the new vector has all the attributes from both Person and Company.


The constraint is, if the Id matches all the fields in Member object will be populated. If the id doesn't match, only the corresponding fields of the Member object will be populated.

Is there a easy way to compare each element of the 2 vectors? I tried a simple for loop iterating through the elements but its not working out right.

Any help is appreciated.
Thanks
A.R
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aravind:

The constraint is, if the Id matches



Whose id are you matching with who's ?

Assuming Member object's id with the id of Person and Company objetct's, then what does the following statement mean?

If the id doesn't match, only the corresponding fields of the Member object will be populated.



Can you please elaborate?
 
Aravind Ramanujam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i wasn't clear the first time. Hope the below example will make my question clear:

person1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL)
person2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA)
person3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR)

comp1(Id:1, Company:XYZ, Position: Programmer)
comp3(Id:3, Company:HTC, Position:Sales Manager)
comp10(Id:10, Company:ABC, Position:Manager)

member1(Id:1, Name:John, Addr:3 Main st, City:Chicago, State:IL, Company:XYZ, Position: Programmer)
member2(Id:2, Name:Mike, Addr:12 6th st, City:San Francisco, State:CA, Company:, Position: )
member3(Id:3, Name: Diana, Addr:1635 Auburn Bl, City: Portland, State: OR, Company:HTC, Position:Sales Manager)
member10(Id:10, Name:, Addr:, City:, State:, Company:ABC, Position:Manager)

Information for Ids 1 & 3 are merged together. ID 2 has only person info and ID 10 has only company info.
[ December 09, 2008: Message edited by: Aravind Ramanujam ]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the two vectors guaranteed to be sorted? (in order of ID, ascending)
 
Aravind Ramanujam
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. They are not.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmph. for some reason, I'm not getting the code formatting. There's lots of extraneous code in here, but it's something you can copy/paste into an IDE and start tinkering with.

I was thinking if they were sorted, there would be a single-pass way to do it. But since they're not, this will do.


The output of the above, with my implementation, is:


The final output is not in the form of a Vector, but you could either post-process it into one, or use a Vector to start with, and instead of a key-value "mapping" in the HashMap, you can use the index of the merge vector itself (when adding to it, you'd be using the add method that inserts into a specified index). This implies then, you'd do *pre* processing, to determine how large your merge vector should be made.
[ December 09, 2008: Message edited by: Mike Curwen ]
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic