import java.util.Arrays; /* Arrays类常用方法: --------------------------------------------------------------- public static String toString (类型[] a) : 将数组元素拼接为带有格式的字符串 public static boolean equals (类型[] a, 类型[] b) : 比较两个数组是否相同 public static int binarySearch(类型[] a, 类型 key) : 查找元素在数组中的索引(二分查找) public static void sort (类型[] a) : 对数组进行默认升序排序 --------------------------------------------------------------- */ public class ArraysDemo { public static void main(String[] args) { int[] arr1 = {11, 22, 33, 44, 55}; int[] arr2 = {11, 22, 33, 44, 55}; //将数组元素拼接为带有格式的字符串 System.out.println(Arrays.toString(arr1)); //比较两个数组是否相同 System.out.println(Arrays.equals(arr1, arr2)); //查找元素在数组中的索引 System.out.println(Arrays.binarySearch(arr1, 33)); System.out.println(Arrays.binarySearch(arr1, 66)); int[] nums = {11, 66, 55, 33, 88, 44}; //二分查找只能在有序的数组上运行 System.out.println(Arrays.binarySearch(nums, 33)); //默认升序排列 Arrays.sort(nums); System.out.println(Arrays.toString(nums)); } } 输出如下: [11, 22, 33, 44, 55] true 2 -6 -2 [11, 33, 44, 55, 66, 88]
Arrays类常用方法
最新推荐文章于 2024-09-02 22:33:56 发布