[HD Pg 0, Sec. 9.2.4 - uses-of-arrays]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
nk2164
Posts: 21
Joined: Mon Sep 30, 2019 9:48 pm
Contact:

[HD Pg 0, Sec. 9.2.4 - uses-of-arrays]

Post by nk2164 »

Having a hard time trying to grasp Arrays.compare and mismatch methods.

I am not understanding what is meant by prefix in the below description from API.

public static int compare( int[] a, int[] b): Compares two int arrays lexicographically. If the two arrays share a common prefix then the lexicographic comparison is the result of comparing two elements, as if by Integer.compare( int, int), at an index within the respective arrays that is the prefix length. Otherwise, one array is a proper prefix of the other and, lexicographic comparison is the result of comparing the two array lengths. A null array reference is considered lexicographically less than a non-null array reference. Two null array references are considered equal. It returns 0 if the first and second array are equal and contain the same elements in the same order; a value less than 0 if the first array is lexicographically less than the second array; and a value greater than 0 if the first array is lexicographically greater than the second array.

public static int mismatch( int[] a, int[] b): Finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found. The index will be in the range of 0 (inclusive) up to the length (inclusive) of the smaller array.the two arrays share a common prefix then the returned index is the length of the common prefix and it follows that there is a mismatch between the two elements at that index within the respective arrays. If one array is a proper prefix of the other then the returned index is the length of the smaller array and it follows that the index is only valid for the larger array. Otherwise, there is no mismatch.

Code: Select all

		int[] a1 = {11};
		int[] a2 = {11,1112,1113,1114};
		
		System.out.println(Arrays.compare(a1, a2));
		System.out.println(Arrays.mismatch(a1, a2));
And this prints out :

Code: Select all

-3
1
In the book prior to this section, you define prefix means the common part of 2 items being compared . For example in comparing '12345' and '1235' , we can say that '123' is the prefix.

I am trying to use that understanding to figure out why this code would give me a '-3' and '1' as answer. I don't get it. Would appreciate some pointers. Thank you.
Last edited by nk2164 on Sat Jan 18, 2020 7:19 pm, edited 1 time in total.

nk2164
Posts: 21
Joined: Mon Sep 30, 2019 9:48 pm
Contact:

Re: [HD Pg 0, Sec. 9.2.4 - uses-of-arrays]

Post by nk2164 »

ok, i think i got it when i read the below line. So when you say prefix in reference to these array methods, we are not checking content of each element, but how many elements match within the array.
Observe that 2 is also the length of the prefix, which is {0, 1}.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 9.2.4 - uses-of-arrays]

Post by admin »

Right, in your case comparison of a1 a2 returns -3 because a1 is a proper prefix of a2 and its length is 3 less than a2.
...Otherwise, one array is a proper prefix of the other and, lexicographic comparison is the result of comparing the two array lengths.
For the same reason, mismatch returns 1 because the length of a1 (which is a proper prefix of a2) is 1.
If one array is a proper prefix of the other then the returned index is the length of the smaller array and it follows that the index is only valid for the larger array.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 29 guests