Arrays常用工具类的基本使用
package day_23; import java.util.Arrays; /** * Arrays常用工具类的基本使用: * 1.输出 * 2.排序 * 3.扩容 * 4.填充 * 5.缩减 * 6.二分查找(前提是数组从小到大排序) */ public class Test { public static void main(String[] args) { int []data={12,34,123,56,56,45,78,67}; // 1)输出 System.out.println(Arrays.toString(data)); // 2)排序 Arrays.sort(data); System.out.println(Arrays.toString(data)); // 3)扩容 data=Arrays.copyOf(data,data.length*2); System.out.println(Arrays.toString(data)); // 4)填充 Arrays.fill(data,7,16,666); System.out.println(Arrays.toString(data)); // 5)数组缩减 data=Arrays.copyOf(data,data.length/2); System.out.println(Arrays.toString(data)); // 6)二分查找(前提是数组由小到大排序 System.out.println("123的下标="+Arrays.binarySearch(data,123)); System.out.println("56的下标="+Arrays.binarySearch(data,56)); System.out.println("67的下标="+Arrays.binarySearch(data,67)); System.out.println("12的下标="+Arrays.binarySearch(data,12)); System.out.println("666的下标="+Arrays.binarySearch(data,666)); } }