Need Help in Collection

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 7
Reputation: priti_s is an unknown quantity at this point 
Solved Threads: 0
priti_s priti_s is offline Offline
Newbie Poster

Need Help in Collection

 
0
  #1
Oct 16th, 2008
Hi
I have come across a question in a collection which says:
One can add a reference of a collection to itself. Is it true or false?
I read the explanation that, we can add the reference to the collection to itself, but it results in the stack overflow of the JVM. Please people tell me what is the correct answer, i am confused because we can add the reference but we should not add it.
Please help me
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Need Help in Collection

 
0
  #2
Oct 16th, 2008
Originally Posted by priti_s View Post
Hi
I have come across a question in a collection which says:
One can add a reference of a collection to itself. Is it true or false?
I read the explanation that, we can add the reference to the collection to itself, but it results in the stack overflow of the JVM. Please people tell me what is the correct answer, i am confused because we can add the reference but we should not add it.
Please help me
Ok, let me see if I understand what you're asking.

Let's first determine what a collection is.

Collection (programming) - a structure that holds many objects of some type (in old versions of Java, collections stored arbitrary objects, and in new versions you can specify a Generic collection that is 'at least' the type specified).

Reference (programming) - An address of the underlying object.

So can we add a Reference of a Collection to a Collection? Sure you can. I believe that is called a composite structure, though I'm no professional so I could be wrong =P

  1.  
  2. /*Declare an ArrayList that can store Object references*/
  3. ArrayList<Object> myArrayList = new ArrayList<Object>(0);
  4.  
  5. // ... some time later in code...
  6.  
  7. /*Adding the address of the ArrayList itself to itself*/
  8. myArrayList.add(myArrayList);

--the only way you can make a collection overflow is if you are creating a custom collection and you attempt to declare a globally scoped Reference to a collection that is the same class (or a derived class) of the class you are defining and use eager-instantiation, or possibly Constructor-instantiation of the collection-reference itself.

You can, instead, provide a way to dynamically set your Collection reference to prevent a stack-overflow.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 7
Reputation: priti_s is an unknown quantity at this point 
Solved Threads: 0
priti_s priti_s is offline Offline
Newbie Poster

Re: Need Help in Collection

 
0
  #3
Oct 16th, 2008
Originally Posted by Alex Edwards View Post
Ok, let me see if I understand what you're asking.

Let's first determine what a collection is.

Collection (programming) - a structure that holds many objects of some type (in old versions of Java, collections stored arbitrary objects, and in new versions you can specify a Generic collection that is 'at least' the type specified).

Reference (programming) - An address of the underlying object.

So can we add a Reference of a Collection to a Collection? Sure you can. I believe that is called a composite structure, though I'm no professional so I could be wrong =P

  1.  
  2. /*Declare an ArrayList that can store Object references*/
  3. ArrayList<Object> myArrayList = new ArrayList<Object>(0);
  4.  
  5. // ... some time later in code...
  6.  
  7. /*Adding the address of the ArrayList itself to itself*/
  8. myArrayList.add(myArrayList);

--the only way you can make a collection overflow is if you are creating a custom collection and you attempt to declare a globally scoped Reference to a collection that is the same class (or a derived class) of the class you are defining and use eager-instantiation, or possibly Constructor-instantiation of the collection-reference itself.

You can, instead, provide a way to dynamically set your Collection reference to prevent a stack-overflow.
Hey, thanks, but from exam point of view, what shall i write True or False? From examples it is clear that surely we can add, but it's not recommended, isn't it? So shall i go with option "True"?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Need Help in Collection

 
0
  #4
Oct 16th, 2008
Originally Posted by priti_s View Post
Hey, thanks, but from exam point of view, what shall i write True or False? From examples it is clear that surely we can add, but it's not recommended, isn't it? So shall i go with option "True"?
I don't believe Menus or certain 'Views' would be possible without some type of Composite structure, so a reference to a Collection (or more appropriately, a Composite) within a Collection is good for specific uses.

It's hard to argue what's right or wrong in the Exam perspective. It depends on how the question is worded.

But since you're just adding a reference to a Collection of the same type, you can think of it as a Composite List of some sort. I don't see any harm in it as long as the reference is set dynamically.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need Help in Collection

 
0
  #5
Oct 16th, 2008
Originally Posted by priti_s View Post
Hey, thanks, but from exam point of view, what shall i write True or False? From examples it is clear that surely we can add, but it's not recommended, isn't it? So shall i go with option "True"?
You should answer your exam question based upon your understanding of the subject.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC