• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collection frramework  RSS feed

 
anita dhar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am having some elements in a collection framework , i want to copy all these elemnts in another collection what is the method for it ? This questions asked me during my interview

Thanks in advance
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the javadoc for Collection, one of the first methods is addAll.
[ October 05, 2007: Message edited by: bart zagers ]
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think the question is really about what to consider when you are copying something - the difference between a deep and a shallow copy.

Lets say I have an ArrayList of Schools that I wish to copy. Each school is composed of Students, Classrooms, etc, etc - do I want to copy the students and classrooms too? Each classroom has a set of desks - do those get copied?

I don't think they were looking for you to write them a method - they were wanting you to be aware of object orientation and it's impact on the copying of an object, or collection.

Hope this helps,

Regards
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confusing copy and clone. A Collection object just contains references to each of the objects. If you copy the contents of one Collection to another, the second Collection also just contains references. No new objects are created.
 
Adam Schaible
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
I think you are confusing copy and clone. A Collection object just contains references to each of the objects. If you copy the contents of one Collection to another, the second Collection also just contains references. No new objects are created.


If someone asks you that in an interview, you should ask for clarification - because the words and their definitions are very similar. Any object in java simply contains references, Object is a reference type.

You can be picky about the verbage for the sake of being picky and tedious, or you can try to answer his question. I'm not sure I could work for a company if the best interview question they could think of is how to copy a collection - just copy references?

Quality post Joanne
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Schaible:


If someone asks you that in an interview, you should ask for clarification - because the words and their definitions are very similar. Any object in java simply contains references, Object is a reference type.

You can be picky about the verbage for the sake of being picky and tedious, or you can try to answer his question. I'm not sure I could work for a company if the best interview question they could think of is how to copy a collection - just copy references?

Quality post Joanne



The question had already been answered by Bart. I just thought that your post might have confused the OP and was just trying to clarify things a little. If you thought I was being picky and tedious, then that wasn't my intention. In order to maintain the Javaranch policy of Be Nice, I think we should leave it at that.
 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adam Schaible:
I would think the question is really about what to consider when you are copying something - the difference between a deep and a shallow copy.

Lets say I have an ArrayList of Schools that I wish to copy. Each school is composed of Students, Classrooms, etc, etc - do I want to copy the students and classrooms too? Each classroom has a set of desks - do those get copied?


Regards


May i know the answers to the above quesstions please.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to supply the answers to the questions. Then you can work out how to implement them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!