File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Implementing one to many relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing one to many relationships" Watch "Implementing one to many relationships" New topic

Implementing one to many relationships

John Uhlig

Joined: May 12, 2003
Posts: 13
Let's say I have a Person class and an Address class. Assume that one person can have multiple addresses. I am thinking that I would put an array or a collection variable on the person and then populate it with Address objects. Is this the generally accepted way of doing it or is there something I am missing?

FYI - I am tyring to create a very simple Rolodex type program just to teach myself some Java skills.

Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Sure, you could do that. A class data member can be another class. I'd consider using a scalable array, such as ArrayList, but there is some additional overhead. For a Rolodex type of progam, the overhead wouldn't really matter much unless your targeting portable devices like Palm or CE.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
I agree. Here's the link:
subject: Implementing one to many relationships
It's not a secret anymore!