About Question enthuware.ocpjp.i.v11.2.3103 :
Moderator: admin
-
- Posts: 10
- Joined: Fri Jun 28, 2019 10:58 am
- Contact:
About Question enthuware.ocpjp.i.v11.2.3103 :
What will the following code print?
char[] a = { 'h', 'e', 'l', 'l'};
char[] b = { };
int x = Arrays.compare(a, b);
int y = Arrays.mismatch(a, b);
System.out.println(x+" "+y);
Can you please explain what does it mean by proper prefix, and why does it return proper prefix ?
Thanks
char[] a = { 'h', 'e', 'l', 'l'};
char[] b = { };
int x = Arrays.compare(a, b);
int y = Arrays.mismatch(a, b);
System.out.println(x+" "+y);
Can you please explain what does it mean by proper prefix, and why does it return proper prefix ?
Thanks
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
Basically:
Common prefix for a pair of arrays is the longest array, which is the prefix of both the arrays. As an example, longest common prefix of "abcdefgh" and "abcefgh" is "abc".
Proper prefix means the prefix is not same as the whole array itself. It will be shorter. So, abcd is a prefix of abcd but it is not a proper prefix. abc is a proper prefix of abcd.
The precise meaning of common and proper prefix is explained in [url=https://docs.oracle.com/en/java/javase/ ... %5D)Arrays javadoc[/url]:
Common prefix for a pair of arrays is the longest array, which is the prefix of both the arrays. As an example, longest common prefix of "abcdefgh" and "abcefgh" is "abc".
Proper prefix means the prefix is not same as the whole array itself. It will be shorter. So, abcd is a prefix of abcd but it is not a proper prefix. abc is a proper prefix of abcd.
The precise meaning of common and proper prefix is explained in [url=https://docs.oracle.com/en/java/javase/ ... %5D)Arrays javadoc[/url]:
Two non-null arrays, a and b with specified ranges [aFromIndex, atoIndex) and [bFromIndex, btoIndex) respectively, share a common prefix of length pl if the following expression is true:
pl >= 0 &&
pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
a[aFromIndex + pl] != b[bFromIndex + pl]
Note that a common prefix length of 0 indicates that the first elements from each array mismatch.
Two non-null arrays, a and b with specified ranges [aFromIndex, atoIndex) and [bFromIndex, btoIndex) respectively, share a proper if the following expression is true:
(aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
"It returns 0 if the first and second array are equal and contain the same elements in the same order" - isn't it pleonasm? I mean, can arrays be equal and not contain the same element in the same order?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
No, I think it is a typo but it is there in official JavaDoc as well. I think it should say "or" i.e. "It returns 0 if the first and second array are equal OR contain the same elements in the same order".
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
Still don't get it. How can arrays be equal and don't meet the second condition - so don't contain the same elements in the same order?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
As I said, it is a typo (and not a pleonasm). It should be OR instead of AND (that means it is talking about two different situations - 1. arrays are equal and 2. if they contain same elements in the same order) .
So, yes, if arrays are equal then there elements have to be equal because it is the same array object.
So, yes, if arrays are equal then there elements have to be equal because it is the same array object.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
I'm sorry I still don't understand, but how are those two situations you mentioned here different?
Could you please give an example? Maybe that will help me.
Could you please give an example? Maybe that will help me.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
char[] a = { 'h', 'e', 'l', 'l'};
char[] b = a; //a and b are equal because they point to the same array
char[] c = {'h', 'e', 'l', 'l'}; //a and c are not equal but they contain the same elements in the same order.
char[] b = a; //a and b are equal because they point to the same array
char[] c = {'h', 'e', 'l', 'l'}; //a and c are not equal but they contain the same elements in the same order.
If you like our products and services, please help us by posting your review here.
-
- Posts: 61
- Joined: Sat Feb 15, 2020 8:56 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3103 :
Oh, ok. That was really helpful and I finally get it. Thanks a lot!
Who is online
Users browsing this forum: No registered users and 58 guests