编程语言
首页 > 编程语言> > 20200116 java.util.Arrays

20200116 java.util.Arrays

作者:互联网

java.util.Arrays

基本信息

公共方法

静态方法

静态方法
void sort(int[] a)
void sort(int[] a, int fromIndex, int toIndex)
void sort(long[] a)
void sort(long[] a, int fromIndex, int toIndex)
void sort(short[] a)
void sort(short[] a, int fromIndex, int toIndex)
void sort(char[] a)
void sort(char[] a, int fromIndex, int toIndex)
void sort(byte[] a)
void sort(byte[] a, int fromIndex, int toIndex)
void sort(float[] a)
void sort(float[] a, int fromIndex, int toIndex)
void sort(double[] a)
void sort(double[] a, int fromIndex, int toIndex)
void sort(Object[] a)
void sort(Object[] a, int fromIndex, int toIndex)
<T> void sort(T[] a, Comparator<? super T> c)
<T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
范围排序时包前不报后,后索引取值最大为数组长度值
void parallelSort(byte[] a)
void parallelSort(byte[] a, int fromIndex, int toIndex)
void parallelSort(char[] a)
void parallelSort(char[] a, int fromIndex, int toIndex)
void parallelSort(short[] a)
void parallelSort(short[] a, int fromIndex, int toIndex)
void parallelSort(int[] a)
void parallelSort(int[] a, int fromIndex, int toIndex)
void parallelSort(long[] a)
void parallelSort(long[] a, int fromIndex, int toIndex)
void parallelSort(float[] a)
void parallelSort(float[] a, int fromIndex, int toIndex)
void parallelSort(double[] a)
void parallelSort(double[] a, int fromIndex, int toIndex)
<T extends Comparable<? super T>> void parallelSort(T[] a)
<T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex)
<T> void parallelSort(T[] a, Comparator<? super T> cmp)
<T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
并行排序
<T> void parallelPrefix(T[] array, BinaryOperator<T> op)
<T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)
void parallelPrefix(long[] array, LongBinaryOperator op)
void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)
void parallelPrefix(double[] array, DoubleBinaryOperator op)
void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)
void parallelPrefix(int[] array, IntBinaryOperator op)
void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)
执行累积方法,第一位和第二位作为左右两个值执行二元方法得到的值作为左与第三位作为右继续执行
int binarySearch(long[] a, long key)
int binarySearch(long[] a, int fromIndex, int toIndex, long key)
int binarySearch(int[] a, int key)
int binarySearch(int[] a, int fromIndex, int toIndex, int key)
int binarySearch(short[] a, short key)
int binarySearch(short[] a, int fromIndex, int toIndex, short key)
int binarySearch(char[] a, char key)
int binarySearch(char[] a, int fromIndex, int toIndex, char key)
int binarySearch(byte[] a, byte key)
int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
int binarySearch(double[] a, double key)
int binarySearch(double[] a, int fromIndex, int toIndex, double key)
int binarySearch(float[] a, float key)
int binarySearch(float[] a, int fromIndex, int toIndex, float key)
int binarySearch(Object[] a, Object key)
int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
<T> int binarySearch(T[] a, T key, Comparator<? super T> c)
<T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
需要数组已排序
如果数组包含多个具有指定值的元素,则无法保证找到哪个元素
当且仅当找到键时返回值 >=0
boolean equals(long[] a, long[] a2)
boolean equals(int[] a, int[] a2)
boolean equals(short[] a, short a2[])
boolean equals(char[] a, char[] a2)
boolean equals(byte[] a, byte[] a2)
boolean equals(boolean[] a, boolean[] a2)
boolean equals(double[] a, double[] a2)
boolean equals(float[] a, float[] a2)
boolean equals(Object[] a, Object[] a2)
数组相等比较,对象数组时通过 equals 方法比较
boolean deepEquals(Object[] a1, Object[] a2)
此方法适用于任意深度的 嵌套数组
void fill(long[] a, long val)
void fill(long[] a, int fromIndex, int toIndex, long val)
void fill(int[] a, int val)
void fill(int[] a, int fromIndex, int toIndex, int val)
void fill(short[] a, short val)
void fill(short[] a, int fromIndex, int toIndex, short val)
void fill(char[] a, char val)
void fill(char[] a, int fromIndex, int toIndex, char val)
void fill(byte[] a, byte val)
void fill(byte[] a, int fromIndex, int toIndex, byte val)
void fill(boolean[] a, boolean val)
void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
void fill(double[] a, double val)
void fill(double[] a, int fromIndex, int toIndex,double val)
void fill(float[] a, float val)
void fill(float[] a, int fromIndex, int toIndex, float val)
void fill(Object[] a, Object val)
void fill(Object[] a, int fromIndex, int toIndex, Object val)
向数组内填充值,如果数组内已有值,则被覆盖
byte[] copyOf(byte[] original, int newLength)
short[] copyOf(short[] original, int newLength)
int[] copyOf(int[] original, int newLength)
long[] copyOf(long[] original, int newLength)
char[] copyOf(char[] original, int newLength)
float[] copyOf(float[] original, int newLength)
double[] copyOf(double[] original, int newLength)
boolean[] copyOf(boolean[] original, int newLength)
<T> T[] copyOf(T[] original, int newLength)
<T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
复制数组值到新的数组中,如果长度大于原数组长度,则大于原数组长度的部分为初始值
<T> T[] copyOfRange(T[] original, int from, int to)
<T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
byte[] copyOfRange(byte[] original, int from, int to)
short[] copyOfRange(short[] original, int from, int to)
int[] copyOfRange(int[] original, int from, int to)
long[] copyOfRange(long[] original, int from, int to)
char[] copyOfRange(char[] original, int from, int to)
float[] copyOfRange(float[] original, int from, int to)
double[] copyOfRange(double[] original, int from, int to)
boolean[] copyOfRange(boolean[] original, int from, int to)
将指定数组的指定范围复制到新数组中,包前不报后,如果 from == to ,返回空数组
<T> List<T> asList(T... a)
返回由指定数组支持的固定大小的列表。 与 Collection.toArray 结合使用,此方法充当基于数组的 API 和基于集合的 API 之间的桥梁。 返回的实际上是 ArrayList
注意基本类型和对象的区别
List<int[]> intsList = Arrays.asList(ints);
List<String> strList = Arrays.asList(strs);
int hashCode(long a[])
int hashCode(int a[])
int hashCode(short a[])
int hashCode(char a[])
int hashCode(byte a[])
int hashCode(boolean a[])
int hashCode(float a[])
int hashCode(double a[])
int hashCode(Object a[])
返回对象的哈希码值。支持此方法是为了使哈希表受益,例如 java.util.HashMap 提供的哈希表。
int deepHashCode(Object a[])
根据指定数组的“深层内容”返回哈希码。
String toString(long a[])
String toString(int a[])
String toString(short a[])
String toString(char a[])
String toString(byte a[])
String toString(boolean a[])
String toString(float a[])
String toString(double a[])
String toString(Object a[])
更美观的数组 toString 描述
String deepToString(Object[] a)
支持多维数组
<T> void setAll(T[] array, IntFunction<? extends T> generator)
void setAll(int[] array, IntUnaryOperator generator)
void setAll(long[] array, IntToLongFunction generator)
void setAll(double[] array, IntToDoubleFunction generator)
使用提供的生成器函数来计算每个元素,设置指定数组的所有元素,生成器接受索引 并为该位置产生所需值的函数
<T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)
void parallelSetAll(int[] array, IntUnaryOperator generator)
void parallelSetAll(long[] array, IntToLongFunction generator)
void parallelSetAll(double[] array, IntToDoubleFunction generator)
并行执行
<T> Spliterator<T> spliterator(T[] array)
<T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive)
Spliterator.OfInt spliterator(int[] array)
Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive)
Spliterator.OfLong spliterator(long[] array)
Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive)
Spliterator.OfDouble spliterator(double[] array)
Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive)
返回一个 Spliterator 对象
<T> Stream<T> stream(T[] array)
<T> Stream<T> stream(T[] array, int startInclusive, int endExclusive)
IntStream stream(int[] array)
IntStream stream(int[] array, int startInclusive, int endExclusive)
LongStream stream(long[] array)
LongStream stream(long[] array, int startInclusive, int endExclusive)
DoubleStream stream(double[] array)
DoubleStream stream(double[] array, int startInclusive, int endExclusive)
返回一个 Stream 对象

示例代码

@Test
public void test() {
    int[] ints = {2, 5, 4, 3, 1};
    System.out.println("int[] ints 原数组 :: " + Arrays.toString(ints));   // int[] ints 原数组 :: [2, 5, 4, 3, 1]

    int[] newInts = Arrays.copyOf(ints, ints.length);
    Arrays.sort(newInts);
    System.out.println("sort(int[] a) :: " + Arrays.toString(newInts)); // sort(int[] a) :: [1, 2, 3, 4, 5]

    int[] newInts2 = Arrays.copyOf(ints, ints.length);
    Arrays.sort(newInts2, 1, 4);    
    System.out.println("sort(int[] a, int fromIndex, int toIndex) :: " + Arrays.toString(newInts2));    // // sort(int[] a, int fromIndex, int toIndex) :: [2, 3, 4, 5, 1]


    int[] newInts3 = Arrays.copyOf(ints, ints.length);
    Arrays.parallelSort(newInts3);
    System.out.println("parallelSort(int[] a) :: " + Arrays.toString(newInts3));    // parallelSort(int[] a) :: [1, 2, 3, 4, 5]

    int[] newInts4 = Arrays.copyOf(ints, ints.length);
    Arrays.parallelPrefix(newInts4, (left, right) -> left + right);
    System.out.println("parallelPrefix(int[] array, IntBinaryOperator op) :: " + Arrays.toString(newInts4));    // parallelPrefix(int[] array, IntBinaryOperator op) :: [2, 7, 11, 14, 15]

    int i = Arrays.binarySearch(newInts, 8);
    System.out.println("binarySearch(int[] a, int key) :: " + i);   // binarySearch(int[] a, int key) :: -6

    boolean equals = Arrays.equals(ints, new int[]{2, 5, 4, 3, 1});
    System.out.println("equals(int[] a, int[] a2) :: " + equals);   // equals(int[] a, int[] a2) :: true

    boolean b = Arrays.deepEquals(new int[][]{ints, ints}, new int[][]{ints, ints});
    System.out.println("deepEquals(Object[] a1, Object[] a2) :: " + b); // deepEquals(Object[] a1, Object[] a2) :: true

    int[] newInts5 = new int[10];
    Arrays.fill(newInts5, 1);
    System.out.println("fill(int[] a, int val) :: " + Arrays.toString(newInts5));   // fill(int[] a, int val) :: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

    int[] newInts6 = new int[10];
    Arrays.fill(newInts6, 1, 10, 2);
    System.out.println("fill(int[] a, int val) :: " + Arrays.toString(newInts6));   // fill(int[] a, int val) :: [0, 2, 2, 2, 2, 2, 2, 2, 2, 2]

    int[] newInts7 = Arrays.copyOf(ints, 3);
    System.out.println("copyOf(int[] original, int newLength) :: " + Arrays.toString(newInts7));    // copyOf(int[] original, int newLength) :: [2, 5, 4]
    int[] newInts8 = Arrays.copyOf(ints, 8);
    System.out.println("copyOf(int[] original, int newLength) :: " + Arrays.toString(newInts8));    // copyOf(int[] original, int newLength) :: [2, 5, 4, 3, 1, 0, 0, 0]

    int[] newInts9 = Arrays.copyOfRange(ints, 1, 1);
    System.out.println("copyOfRange(int[] original, int from, int to) :: " + Arrays.toString(newInts9));    // copyOfRange(int[] original, int from, int to) :: [5, 4]

    String[] strs = {"2", "5", "4", "3", "1"};
    List<int[]> intsList = Arrays.asList(ints);
    List<String> strList = Arrays.asList(strs);
    System.out.println("asList(T... a) :: " + intsList);    // asList(T... a) :: [[I@2f333739]
    System.out.println("asList(T... a) :: " + strList);     // asList(T... a) :: [2, 5, 4, 3, 1]


    System.out.println("hashCode(int a[]) :: " + Arrays.hashCode(ints));    // hashCode(int a[]) :: 30629086

    System.out.println("deepToString(Object[] a) :: " + Arrays.deepToString(new int[][]{ints, newInts2}));  // deepToString(Object[] a) :: [[2, 5, 4, 3, 1], [2, 3, 4, 5, 1]]

    int[] newInts10 = Arrays.copyOf(ints, ints.length);
    Arrays.setAll(newInts10, o -> newInts10[o] * newInts10[o]);
    System.out.println("setAll(int[] array, IntUnaryOperator generator) :: " + Arrays.toString(newInts10)); // setAll(int[] array, IntUnaryOperator generator) :: [4, 25, 16, 9, 1]


    int[] newInts11 = Arrays.copyOf(ints, ints.length);
    Arrays.parallelSetAll(newInts11, o -> newInts11[o] * newInts11[o]);
    System.out.println("parallelSetAll(int[] array, IntUnaryOperator generator) :: " + Arrays.toString(newInts11)); // parallelSetAll(int[] array, IntUnaryOperator generator) :: [4, 25, 16, 9, 1]


    Spliterator.OfInt spliterator = Arrays.spliterator(ints);

    IntStream stream = Arrays.stream(ints);

    System.out.println("原数组:" + Arrays.toString(ints)); // 原数组:[2, 5, 4, 3, 1]

}

标签:toIndex,java,int,void,fromIndex,util,Arrays,20200116,array
来源: https://www.cnblogs.com/huangwenjie/p/14291891.html