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

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

/*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.

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

/*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"?

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.

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.

This question has already been answered. Start a new discussion instead.