I am trying to code my own version of ArrayList (to learn how it works) and I was looking at the Cloneable interface which is implemented by java.lang.ArrayList (I would like to keep the same contract than the original version).
I am a bit confused by the definition of the clone() method and hope someone can clarify it for my case.
The part I am not really sure of, is that if someone use the clone() method on my collection, it should return a new ArrayList (meaning not a reference to the existing one). However:
- Does it means that each object contained needs to be cloned as well?
- Do I have to explicitly create a new instance using 'new' for each of them (which should be quite slow?)
- Should I be trying to use the clone() method on my collection's objects, and how to be sure they override the clone() method as the definition in Cloneable states that it is not explicitly required (the method is not in the Interface)?
Thanks in advance for any help.