【集合】Arrays工具类
一、Arrays定义
java.util.Arrays是一个包装类,它包含了各种 数组操作 的 静态多态方法。此类 不能实例化,就像一个 工具类,服务于Java中的数组。
其定义:
public class Arrays {
....
}
二、Arrays常用静态方法
1. 数组排序
(1)int[] 数组升序排序
public static void sort(int[] a)
(2)int[]数组指定范围 升序排序
排序范围是从元素下标为fromIndex,到下标为toIndex-1的元素进行排序。
public static void sort(int[] a, int fromIndex, int toIndex)
2.其他基础类型的数组排序
public static void sort(char[] a)
public static void sort(char[] a, int fromIndex, int toIndex)
public static void sort(byte[] a)
public static void sort(byte[] a, int fromIndex, int toIndex)
public static void sort(short[] a)
public static void sort(short[] a, int fromIndex, int toIndex)
public static void sort(long[] a)
public static void sort(long[] a, int fromIndex, int toIndex)
public static void sort(float[] a)
public static void sort(float[] a, int fromIndex, int toIndex)
public static void sort(double[] a)
public static void sort(double[] a, int fromIndex, int toIndex)
3. 指定排序算法进行排序
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a);
} else {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}
public interface Comparator<T> {
//a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
int compare(T o1, T o2);
}
使用:
public int xxxxx(int[] A) {
int N = A.length;
Integer[] B = new Integer[N];
for (int i = 0; i < N; ++i)
B[i] = i;
//把B中保存的i,按照i对应的A中的值排升序
Arrays.sort(B, (i, j) -> ((Integer) A[i]).compareTo(A[j]));
...
}
4. Java 8新增的并行排序
java8新增的并行排序算法,基于fork/join框架。
public static void parallelSort(char[] a)
public static void parallelSort(char[] a, int fromIndex, int toIndex)
public static void parallelSort(byte[] a)
public static void parallelSort(byte[] a, int fromIndex, int toIndex)
public static void parallelSort(short[] a)
public static void parallelSort(short[] a, int fromIndex, int toIndex)
public static void parallelSort(int[] a)
public static void parallelSort(int[] a, int fromIndex, int toIndex)
public static void parallelSort(long[] a)
public static void parallelSort(long[] a, int fromIndex, int toIndex)
public static void parallelSort(float[] a)
public static void parallelSort(float[] a, int fromIndex, int toIndex)
public static void parallelSort(double[] a)
public static void parallelSort(double[] a, int fromIndex, int toIndex)
5.数组中所有字段填充为指定值
以填充int[]类型数组为为例,其实现方法为:
public static void fill(int[] a, int val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}
其他类似的填充方法:
public static void fill(boolean[] a, boolean val)
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
public static void fill(char[] a, char val)
public static void fill(char[] a, int fromIndex, int toIndex, char val)
public static void fill(byte[] a, byte val)
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
public static void fill(short[] a, short val)
public static void fill(short[] a, int fromIndex, int toIndex, short val)
public static void fill(int[] a, int val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static void fill(long[] a, long val)
public static void fill(long[] a, int fromIndex, int toIndex, long val)
public static void fill(float[] a, float val)
public static void fill(float[] a, int fromIndex, int toIndex, float val)
public static void fill(double[] a, double val)
public static void fill(double[] a, int fromIndex, int toIndex,double val)
6.数组Copy
(1)指定长度,把入参数组的内容复制为副本,返回副本
基础类型数组copyOf方法:
public static byte[] copyOf(byte[] original, int newLength) {
byte[] copy = new byte[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static boolean[] copyOf(boolean[] original, int newLength)
public static char[] copyOf(char[] original, int newLength)
public static short[] copyOf(short[] original, int newLength)
public static int[] copyOf(int[] original, int newLength)
public static long[] copyOf(long[] original, int newLength)
public static float[] copyOf(float[] original, int newLength)
public static double[] copyOf(double[] original, int newLength)
对象类型数组copyOf方法:
public static <T> T[] copyOf(T[] original, int newLength)
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
(2)指定起、止索引,把入参数组的内容复制为副本,返回副本
基础类型数组copyOfRange方法:
public static boolean[] copyOfRange(boolean[] original, int from, int to)
public static char[] copyOfRange(char[] original, int from, int to)
public static byte[] copyOfRange(byte[] original, int from, int to)
public static short[] copyOfRange(short[] original, int from, int to)
public static int[] copyOfRange(int[] original, int from, int to)
public static long[] copyOfRange(long[] original, int from, int to)
public static float[] copyOfRange(float[] original, int from, int to)
public static double[] copyOfRange(double[] original, int from, int to)
对象类型数组copyOfRange方法:
public static <T> T[] copyOfRange(T[] original, int from, int to)
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
(3)指定范围Copy数组(在System中定义)
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
7.数组转换成字符串
(1)一维数组转换成字符串
public static String toString(boolean[] a)
{
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
其他类型数组类似方法:
public static String toString(char[] a)
public static String toString(byte[] a)
public static String toString(short[] a)
public static String toString(int[] a)
public static String toString(long[] a)
public static String toString(float[] a)
public static String toString(double[] a)
public static String toString(Object[] a)
(2)多维数组转换成字符串
public static String deepToString(Object[] a)
8.数组转换成ArrayList
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
样例:
List intList1 = Arrays.asList(1,4,7,11,15);
Arrays.asList()将一个数组转化为一个ArrayList。
这个方法会返回一个ArrayList类型的对象,这个ArrayList类并非java.util.ArrayList类,而是Arrays类的静态内部类!对这个对象的列表进行添加、删除、更新操作,就会报UnsupportedOperationException异常。
所以,如下样例中:
- 第一句:是新创建了1个对象,通过Arrays类的静态内部类对象初始化。所以,list1 列表是可添加、删除操作的。
- 第二句:list2直接引用通过Arrays类的静态内部类对象。所以,添加、删除操作会报异常;但是可以修改对象中的内容。
List<String> list1 = new ArrayList<String>(Arrays.asList(args));
List<String> list2 = Arrays.asList("hello", "world");