Saturday, August 25, 2007

Array > Copying An Array

COPYING AN ARRAY
• Arrays are reference types. An array variable contains a reference to an array instance
• Hence, when a copy of an array variable is created, it means same array variable with two references
• This is shown as under:
• int[] pins = {3,6,9,12}; int[] alias = pins;
• Here, alias is not a separate array, but a reference to an array pointing to array called pins
• To create a copy of an array instance, two separate arrays must be declared with same data type and length. Finally, copy the values from one array into another
• This process is shown as under:
int[] pins = {9,3,7,2};
int[] copy = new int[pins.Length];

for(int i =0; i != copy.Length; i++)
copy[i] = pins[i];

No comments: