Array Equality
Explore how to determine if two Java arrays are equal by comparing their elements individually or using built-in methods like Arrays.equals. Understand the distinction between array equality and identity, and learn how to implement your own method for checking equality of string arrays.
We'll cover the following...
The meaning of array equality
If we have two distinct arrays, what does it mean to say that the arrays are equal? Arrays are equal when their corresponding entries are equal. For example, the following two arrays are equal:
String[] names = {"alpha", "beta", "gamma", "delta"};
String[] tags = {"alpha", "beta", "gamma", "delta"};
The arrays have the same length, and the strings in corresponding elements are equal in value. That is, names[index] equals tags[index] as index ranges in value from 0 to 3. Since the arrays contain strings, we could test the equality of these entries by evaluating the Boolean expression
names[index].equals(tags[index]) for all values of index. We will do this in the next segment.
Testing for array equality
We can write our ...