Array Mismatch

The Arrays.mismatch() static method returns the first index where a mismatch occurs between two arrays. If there is no mismatch, the arrays are equal, and the value -1 is returned. The index returned is determined by the prefix of the arrays. The discussion on common and proper prefixes in the subsection on comparing arrays is highly relevant for determining mismatch (p. 869).

Click here to view code image

int mismatch(
type
[] a,
type
[] b)

The permitted type for elements includes all primitive types (boolean, byte, char, double, float, int, long, short) and Object.

This method returns the index of the first mismatch between two arrays, where 0 <= index <= Math.min(a.length, b.length). Otherwise, it returns -1 if no mismatch is found.

The method throws a NullPointerException if either of the arrays is null.

The examples below illustrate how the mismatch is determined.

The two arrays fruitBasketA and fruitBasketC have a common prefix [oranges, apples]. Mismatch therefore occurs at the index given by the length of the prefix (2). The elements fruitBasketA[2] and fruitBasketC[2] mismatch—that is, the element strings are not equal. The mismatch() method returns the value 2, the same as the length of the common prefix.

Click here to view code image

Index                0       1       2     3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketC ==> [oranges, apples, kiwi, plums]
Common prefix: [oranges, apples]
Mismatch index: 2

In the following example, the common prefix is empty. Its length is 0. The elements at index 0, fruitBasketA[0] and fruitBasketG[0], mismatch. The index 0 is returned.

Click here to view code image

Index                0       1       2     3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketG ==> [apples]
Common prefix: []
Mismatch index: 0

In the next example, the array lengths are equal and the prefix is the same as both arrays. There is no mismatch between the two arrays. The value -1 is returned.

Click here to view code image

Index                0       1       2     3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketB ==> [oranges, apples, plums, kiwi]
Prefix: [oranges, apples, plums, kiwi]
Mismatch value: -1

In the example below, the arrays have a proper prefix, [oranges, apples], as the prefix is the same as one of the arrays and the arrays have different lengths. Mismatch therefore occurs at the index given by the length of the proper prefix (2) in the longer array, fruitBasketA. Index 2 is only valid in this array. The mismatch() method returns the index 2.

Click here to view code image

Index                0       1       2     3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketE ==> [oranges, apples]
Proper prefix: [oranges, apples]
Mismatch index: 2

The following code can be used to demonstrate the index value returned by the mismatch() method in the examples above, where System.out is statically imported:

Click here to view code image

String[] fruitBasketA = { “oranges”, “apples”, “plums”, “kiwi” };
String[] fruitBasketB = { “oranges”, “apples”, “plums”, “kiwi” };
String[] fruitBasketC = { “oranges”, “apples”, “kiwi”, “plums” };
String[] fruitBasketE = { “oranges”, “apples” };
String[] fruitBasketG = { “apples” };

out.println(“Mismatch index: “+Arrays.mismatch(fruitBasketA, fruitBasketC)); //2
out.println(“Mismatch index: “+Arrays.mismatch(fruitBasketA, fruitBasketG)); //0
out.println(“Mismatch index: “+Arrays.mismatch(fruitBasketA, fruitBasketB)); //-1
out.println(“Mismatch index: “+Arrays.mismatch(fruitBasketA, fruitBasketE)); //2

Leave a Reply

Your email address will not be published. Required fields are marked *