I need a shallow copy of an java ArrayList, should I use clone() or iterate over original list and copy elements in to new arrayList, which is faster ?
I need a shallow copy of an java ArrayList , should I use clone()
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
clone(), or use the copy-constructor.The copy-constructor makes additional transformation from the passed collection to array, while the
clone()method uses the internal array directly.Have in mind that
clone()returnsObject, so you will have to cast toList.