#定义一维数组arr[] = {34,67,24,18,29,33},利用工具类Arrays实现对数组arr的排序,搜索和复制任务。
package demo;
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
//定义一维数组arr[] = {34,67,24,18,29,33},利用工具类Arrays实现对数组arr的排序,搜索和复制任务
Scanner scan = new Scanner(System.in);
int [] arr = {34,67,24,18,29,33};
//排序,按升序排列
Arrays.sort(arr);
System.out.println("Sorted array:" + Arrays.toString(arr));
//搜索
System.out.println("请输入检索数字:");
int searchValue = scan.nextInt();
int index = Arrays.binarySearch(arr,searchValue);
if (index >= 0) {
System.out.println("Element " + searchValue + " found at index: " + index);
} else {
System.out.println("Element " + searchValue + " not found.");
}
//复制
int [] copiedArr = Arrays.copyOf(arr,arr.length);
System.out.println("Copied array: " + Arrays.toString(copiedArr));
}
}