I am trying to do 2 different sorting algorithms on 2 exactly similar arrays (numbers and numbers2) that are generated through the Random class. I declare my 2 arrays and fill them with Random.NextBytes.
After that I do my first algorithm on numbers and then my second sorts on numbers2.
But I notice that numbers2 just seems to be a pointer to numbers because by the time I want to sort numbers2 it is already sorted.
How do I fill numbers2 with exactly the same numbers as numbers? Do I need to do it by hand with a for loop? Thank you!
class FillArray
{
public byte[] numbers;
public byte[] numbers2;
//instantiate MS Random object
Random Generator = new Random();
//Constructor which takes array size
public FillArray(int amountx)
{
numbers = new byte[amountx]
Generator.NextBytes(numbers);
numbers2 = new byte[amountx];
numbers2 = numbers;
amount = amountx;
}
Arrays are reference types, so if you want to clone the array you’ll need to copy it via Array.Copy.
You can also use Array.CopyTo. If you don’t have a pre-existing array you can use the
Clone()method as well to create a new one with shallow copies of all elements.