Arrays常用方法

java.util.Arrays

  • 数组排序
static void sort(Object[] a)
static void sort(Object[] a, int fromIndex, int toIndex)
static <T> void sort(T[] a, Comparator<? super T> c)
static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
  • 数组转String
static String toString(Object[] a)
static String deepToString(Object[] a)
  • 数组填充
static void fill(Object[] a, Object val)
static void fill(Object[] a, int fromIndex, int toIndex, Object val) // [from, to)
  • 数组比较
static boolean equals(Object[] a, Object[] a2)
static boolean deepEquals(Object[] a1, Object[] a2)
  • 数组转List
static <T> List<T> asList(T... a)  // 

这里有两个坑:

  1. 原始数据类型int的数组调用asList之后得到的List只有一个元素,这个元素就是元素类型的数组。而封装类Integer数组调用asList是把数组中每个元素加到了List中。
int[] array = new int[]{3, 10, 4, 0, 2};
List<int[]> ints = Arrays.asList(array);

Integer[] arr = new Integer[]{3, 10, 4, 0, 2};
List<Integer> integers = Arrays.asList(arr);
  1. 返回的ListArrays.ArrayList,不是java.util.ArrayList,不可以增删改,需要再包装一个成 ArrayList。
  • 数组拷贝
static <T> T[] copyOf(T[] original, int newLength)
static <T> T[] copyOfRange(T[] original, int from, int to)
  • 二分查找
static int binarySearch(Object[] a, Object key)
static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
  • 数组的hash值
// Returns a hash code based on the contents of the specified array.
static int hashCode(Object[] a)
原文地址:https://www.cnblogs.com/caophoenix/p/13463713.html