import java.util.*;
public class Quest {
public static void main(String[] args) {
String[] colors =
{ "blue", "red", "green", "yellow", "orange"};
Arrays.sort(colors);
int s2 = Arrays.binarySearch(colors, "orange");
int s3 = Arrays.binarySearch(colors, "violet");
System.out.print(s2 + "" + s3);
}
}
What is the result?
A. 2-1
B. 2-4
C. 2-5
D. 3-1
E. 3-4
F. 3-5
G. Compilation fails.
H. An exception is thrown at runtime.
答案:C
解析:
根據 Java API 的說明文件
使用 Arrays.binarySearch() 方法前
須對陣列先行"排序"(Arrays.sort())
Arrays.binarySearch(被尋找的陣列, 欲尋找的值)
若在被尋找的陣列能夠找到欲尋找的值,其位置是從0開始起算
若無法在被尋找的陣列找到欲尋找的值,其位置就是從-1起算其所應該在的位置
0 意見:
張貼留言