| Author |
Enthuware Map question
|
John Stark
Ranch Hand
Joined: Jul 19, 2011
Posts: 165
|
|
Hi,
One question in Enthuware is:
Which of these statements concerning maps are true?
One option is:
It is permissible for a map to contain itself as a key.
In the review it says this option is wrong 'But it can contain itself as a value'.
I don't really know what that means. Can anybody explain?
Thanks,
John
|
 |
Riza Aktunc
Greenhorn
Joined: Sep 07, 2011
Posts: 24
|
|
A map has two attributes - key and value.
Judging by what you have posted; a map can contain itself as a value but not a key.
String x="";
HashMap hm=new HashMap();
hm.put(hm,x); //not ok
hm.put(x,hm); //ok
But, when I try it, compiler does not complain! In runtime hm.put(hm,x); gives StackOverFlowError!
|
OCPJP - 93%
|
 |
John Stark
Ranch Hand
Joined: Jul 19, 2011
Posts: 165
|
|
So the StackOverFlowError occurs because the map contains itself which contains itself contains itself ...?
Why does this not occur when adding the map as a value?
Thanks,
John
|
 |
 |
|
|
subject: Enthuware Map question
|
|
|