Arrays常用方法

java.util.Arrays

  • 数组排序
Copy
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
Copy
static String toString(Object[] a) static String deepToString(Object[] a)
  • 数组填充
Copy
static void fill(Object[] a, Object val) static void fill(Object[] a, int fromIndex, int toIndex, Object val) // [from, to)
  • 数组比较
Copy
static boolean equals(Object[] a, Object[] a2) static boolean deepEquals(Object[] a1, Object[] a2)
  • 数组转List
Copy
static <T> List<T> asList(T... a) //

这里有两个坑:

  1. 原始数据类型int的数组调用asList之后得到的List只有一个元素,这个元素就是元素类型的数组。而封装类Integer数组调用asList是把数组中每个元素加到了List中。
Copy
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。
  • 数组拷贝
Copy
static <T> T[] copyOf(T[] original, int newLength) static <T> T[] copyOfRange(T[] original, int from, int to)
  • 二分查找
Copy
static int binarySearch(Object[] a, Object key) static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
  • 数组的hash值
Copy
// Returns a hash code based on the contents of the specified array. static int hashCode(Object[] a)
posted @   Recycer  阅读(171)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
点击右上角即可分享
微信分享提示