Here is a short example with Integer:
int [] numbers = { 2, 3, 4, 5};
int [] numbersCopy = numbers;
numbersCopy[2] = 0;
System.out.println(numbers[2]);//Display 0
System.out.println(numbersCopy[2]);//Display 0 !!! Both = Same reference
But if you do :
int [] numbers = { 2, 3, 4, 5};
int [] numbersClone = (int[])numbers.clone();
numbersClone[2] = 0;
System.out.println(numbers[2]);//Display 4
System.out.println(numbersClone[2]);//Display 0 !!! Not the same :)
Hash code of an object is the same with a copy array, when cloning is not.
Amit ChoudharyPosted Apr 29, 2010, 12:44 PM
As much i know you Clone() creates the structure same as the original array or collection. but he CopyTo Copy the data and schema to new collection..
Please mark "Do you like the answe"r if it helps.
Jaish MathewsPosted Apr 29, 2010, 9:05 AM
http://interviews.c-sharpcorner.com/Answer/Answers.aspx?QuestionId=1375&MajorCategoryId=1&MinorCategoryId=16