2009年2月3日 星期二

第10題 SampleCode

1. import java.util.*;
2. public class Quest{
3. public static void main(String[] args){
4. String[] colors =
5. {"blue","red","green","yellow","orange"};
6. Arrays.sort(colors);
7. int s2 = Arrays.binarySearch(colors, "orange");
8. int s3 = Arrays.binarySearch(colors, "violet");
9. System.out.print(s2 + "" + s3);
10. }
11. }
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

import java.util.*;
public class test10 {
public static void main(String[] args){
String[] colors =
{"blue","red","green","yellow","orange"};
Arrays.sort(colors);
for (int i=0;i<colors.length ; i++){
System.out.println(colors[i]);
}
//二分搜尋法
int s2 = Arrays.binarySearch(colors, "orange");
int s3 = Arrays.binarySearch(colors, "violet");
System.out.println(s2 + "" + s3);
//從第一個字母依序往後找
}
}

ANS:
C. 2-5

沒有留言:

張貼留言