Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Arrays to Array

Tags:

arrays

c#

copy

I have a little problem with arrays. I am new in C#.

I try to copy an int array into two other int arrays with

unsortedArray = randomNumbers();

unsortedArray2 = unsortedArray;
unsortedArray3 = unsortedArray;

But, if I sort the unsortedArray2, the unsortedArray3 is sorted too.

Can someone help me?

like image 422
ARX Avatar asked Sep 06 '17 08:09

ARX


People also ask

How do you assign an array to another array?

Ensure that the two arrays have the same rank (number of dimensions) and compatible element data types. Use a standard assignment statement to assign the source array to the destination array. Do not follow either array name with parentheses.

What is clone array in Java?

Using copyOfRange() Method The copyOfRange() method is used to copy the elements of the specified range of the original array into clone array. The syntax of the copyOfRange() method is as follows: public static int[] copyOfRange(int[] original, int from, int to)

Can be used to copy data from one array to another?

copyOfRange() is used to copy a range of values from one array to another in Java. The copyOfRange(T[] original, int from, int to) takes three parameters, the original array, index of the first element or start index, and index of last element or end index.

How do I copy one array to another array in C#?

copy method in C# to copy a section of one array to another. The array. copy() method allow you to add the source and destination array. With that, include the size of the section of the first array that includes in the second array.

How do I copy a specific part of an array?

If you want to copy the first few elements of an array or a full copy of an array, you can use Arrays.copyOf() method. Arrays.copyOfRange() is used to copy a specified range of an array. If the starting index is not 0, you can use this method to copy a partial array.

What is the difference between arraycopy () and clone ()?

System.arraycopy () is faster than clone () as it uses Java Native Interface (Source : StackOverflow) If you want to copy the first few elements of an array or a full copy of an array, you can use Arrays.copyOf () method. Arrays.copyOfRange () is used to copy a specified range of an array.

How to copy a partial array in Java?

If you want to copy the first few elements of an array or a full copy of an array, you can use Arrays.copyOf () method. Arrays.copyOfRange () is used to copy a specified range of an array. If the starting index is not 0, you can use this method to copy a partial array. This article is contributed by Ashutosh Kumar.

When copying from a reference-type array to a value type array?

When copying from a reference-type array to a value-type array, each element is unboxed and then copied. When copying from a value-type array to a reference-type array, each element is boxed and then copied.


2 Answers

I try to copy a Int Array into 2 other Int Arrays with

The first thing that is important is that in this line :

unsortedArray2 = unsortedArray;

you do not copy the values of the unsortedArray into unsortedArray2. The = is called the assignment operator

The assignment operator (=) stores the value of its right-hand operand in the storage location,

Now the second thing that you need to know to understand this phenomenon is that there are 2 types of objects in C# Reference Types and Value types

The documentation explains it actually quite nicely:

Variables of reference types store references to their data (objects), while variables of value types directly contain their data. With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.

The solution can be to use the Array.Copy method.

Array.Copy(unsortedArray, 0, unsortedArray2 , 0, unsortedArray.Length);

The CopyTo method would also work in this case

unsortedArray.CopyTo(unsortedArray2 , 0);

Note:this will work because the content of the array is a value type! If it would be also of reference type, changing a sub value of one of the items would lead also to a change in the same item in the destination array.

like image 144
Mong Zhu Avatar answered Oct 12 '22 12:10

Mong Zhu


The problem which you are struggling is not C# specific. The behavior will be the same almost in any programming language (JS/Java/Python/C). Variables unsortedArray2 and unsortedArray3 point to the same chunk of memory and when you reorder array you just manipulate with this memory piece.

Having this in mind, what do we need to achieve your goal? We need to copy array. It can be done in many ways

  1. Array.Copy
Array.Copy(unsortedArray, 0, unsortedArray2 , 0, unsortedArray.Length);
// The CopyTo method would also work in this case
unsortedArray.CopyTo(unsortedArray2 , 0);
  1. Creating new array and copy elements one by one in for loop
var unsortedArray2 = new int[unsortedArray.Length];
for (int i = 0; i < unsortedArray.Length; i++)
{
    unsortedArray2[i] = unsortedArray[i];
}
  1. Linq
var unsortedArray2 = unsortedArray.Clone();

Note: These operations are shallow copy which would work in your case because the contents are value types, not reference types.

like image 27
Eduard Lepner Avatar answered Oct 12 '22 12:10

Eduard Lepner