About Question com.enthuware.ets.scjp.v6.2.92 :
Moderator: admin
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
About Question com.enthuware.ets.scjp.v6.2.92 :
Still, i'm unable to understand why we get -5 to 3 .Pls, explain in a simpler manner.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.92 :
When you search for a string in a sorted list, there are two possibilities -
1. the string is found : In this case, it will return the index at which it was found.
2. the string is not found: In this case, it will return one more than the index at which this string will be inserted if you try to insert it in the list but with a minus sign.
In the given list, if you search for "xxxx", what will happen? It is not in the list, so the sign will be minus. And if you try to insert it, it will be inserted at index 5. So the return value will be -5.
1. the string is found : In this case, it will return the index at which it was found.
2. the string is not found: In this case, it will return one more than the index at which this string will be inserted if you try to insert it in the list but with a minus sign.
In the given list, if you search for "xxxx", what will happen? It is not in the list, so the sign will be minus. And if you try to insert it, it will be inserted at index 5. So the return value will be -5.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Aug 06, 2017 2:37 am
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.92 :
i understood about -5 but why 3
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.92 :
What all elements are there in the list? What will it return if you search for those elements? Apply the same logic given above.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Tue Jan 11, 2022 4:41 am
Re: About Question com.enthuware.ets.scjp.v6.2.92 :
I cannot think of any string that can come in between a and aa and aaa and aaaa. Could you give an example in which it can give -2, -4, -3, etc?
But if you do this, then you get -1. System.out.println(Arrays.binarySearch(sa, "1"));
According to what I understand, these are the values possible -5 (if higher), -1 (if less), 0, 1, 2, 3
But if you do this, then you get -1. System.out.println(Arrays.binarySearch(sa, "1"));
According to what I understand, these are the values possible -5 (if higher), -1 (if less), 0, 1, 2, 3
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.92 :
Well, you can try searching for these strings - a1, aa1, and aaa1 and see what you get.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 45 guests