| | |
Need Help in Collection
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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
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
java Syntax (Toggle Plain Text)
/*Declare an ArrayList that can store Object references*/ ArrayList<Object> myArrayList = new ArrayList<Object>(0); // ... some time later in code... /*Adding the address of the ArrayList itself to itself*/ 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.
•
•
Join Date: Sep 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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
java Syntax (Toggle Plain Text)
/*Declare an ArrayList that can store Object references*/ ArrayList<Object> myArrayList = new ArrayList<Object>(0); // ... some time later in code... /*Adding the address of the ArrayList itself to itself*/ 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"?
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.
![]() |
Similar Threads
- vb.net newbie game collection (VB.NET)
- Creating an array/collection that holds variables (Visual Basic 4 / 5 / 6)
- How to use collection in java (Java)
- DVD Collection Program (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Java Challenge... Anyone Plz..
- Next Thread: Problem with level by level tree traversal
| Thread Tools | Search this Thread |
2dgraphics android api apple applet application arguments array arrays automation banking binary binarytree bluetooth capture chat chatprogramusingobjects class classes client code color component count database derby design eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui html ide if_statement image input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel julia keyword linux list loop macintosh map method methods midlethttpconnection mobile netbeans newbie object os print printing problem producer program programming project projectideas read recursion reference replaysolutions ria scanner screen server set size sms sort sourcelabs sql stop string swing threads transforms tree ui unicode validation windows






