代码:
import java.util.Arrays;
import java.util.Scanner;
public class practice1_3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]={9,8,7,6,5,4,3,2,1,0};
int brr[]={0,1,2,5,4,3,6,7,8,9};
Scanner reader=new Scanner(System.in);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
Arrays.sort(brr,3,5);
System.out.println(Arrays.toString(brr));
System.out.println("输入要检索的数值:");
while(reader.hasNextInt())
{
int index=reader.nextInt();
int answer=Arrays.binarySearch(arr, index);
if(answer>=0)
System.out.println(index+"和第"+answer+"次检索的结果相同");
else
System.out.println("检索的结果不正确");
}
}
}
说明:1)Arrays.sort(a);//实现了对数组中所有数字的排序
2)Arrays.sort(a,start,end)//start到end-1的排序
3)Arrays.binarySearch(arr, index);//判断数字index是否在数组arr中。
答案: