November 18, 2019
Execute the following code:
int[] arrayA = { 1, 3, 4 };
int[] arrayB = { 1, 3, 4 };
if (arrayA == arrayB){Console.WriteLine("The arrays are the same!");}and see for yourself that you can not use == to test for equality of arrays.
Then, like we did in class:
arrayA and arrayB have the same values.arrayA and arrayB have the same size, and, if they do, display “The arrays are the same!” only if arrayA and arrayB have the same values.true only if the two arrays are identicals (same size, same values).x, and return true only if the two arrays have the same first x values, and false if they are shorter than x or have different values in the first x slots.Once you are done with this, write two other copy methods taking three arguments: one for arrays of decimals, and one for arrays of strings. Can you give them the same name as for the one that takes arrays of integers as arguments? Why, or why not?
Have a look at https://docs.microsoft.com/en-us/dotnet/api/system.array.copy?view=netframework-4.8 and https://stackoverflow.com/q/17212254/ to see other implementations of methods to copy and compare arrays. Note that the solution discussed in the second post requires to use using System.Linq; at the beginning of your program:
using System;
using System.Linq;
class MainClass {
public static void Main (string[] args) {
int[] arrayA = { 1, 8, 9, 10, 1, 30, 1, 32, 3 };
int[] arrayB = { 1, 8, 9, 10, 1, 30, 1, 32, 3 };
int[] arrayC = { 1000000, 8, 9, 10, 1, 30, 1, 32, 3 };
if (arrayA.SequenceEqual(arrayB)){
Console.WriteLine("The arrays arrayA and arrayB are the same!");
}
if (arrayA.SequenceEqual(arrayC)){
Console.WriteLine("The arrays arrayA and arrayC are the same!");
}
}
}