Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b002289

Browse files
committed
Challenge 56.
binarySearch method behaviour.
1 parent 6f33def commit b002289

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package challenge51_60;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* public static int binarySearch(Object[] a, Object key)
7+
* return the index of the searched key in the given array.
8+
* otherwise return the -index -1 ; index value is the position where the element should be inserted.
9+
* the given array should be sorted.
10+
* the elements in the arrays should be comparable.
11+
*
12+
*/
13+
public class Challenge_56 {
14+
static String[] marvel = {"Spiderman","Venom","Carnage","Mysterio"};
15+
16+
public static void main( String[] args ) {
17+
Arrays.sort(marvel);
18+
System.out.println(Arrays.binarySearch(marvel, "Xavier"));
19+
System.out.println(marvel[Arrays.binarySearch(marvel, "Carnage")]);
20+
System.out.println(Arrays.binarySearch(marvel, "Lizard"));
21+
System.out.println(Arrays.binarySearch(marvel, "Apocalypse"));
22+
System.out.println(Arrays.binarySearch(marvel, "Spiderman"));
23+
}
24+
}

0 commit comments

Comments
 (0)