Arrays工具类

说在前面

Arrays类中都是静态方法,没有属性
该类包含用于操作数组(如排序和搜索)的各种方法。该类还包含一个静态工厂,它允许将数组视为列表。
如果指定的数组引用为null,则该类中的方法都会抛出NullPointerException,除非有特别说明。
继承自 java.lang.Object类
since 1.2

所含所有方法

1. static <T> List<T> asList(T... a)  		:返回由指定数组支持的固定大小的列表。(注意固定大小)
2. static int binarySearch(byte[] a, byte key)  	:二分查找算法在byte数组中寻找byte值。
3. static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)  	:二分查找算法在byte数组的制定范围内中寻找byte值,包含fromIndex不包含toIndex。
4. static int binarySearch(char[] a, char key)  
5. static int binarySearch(char[] a, int fromIndex, int toIndex, char key)  
6. static int binarySearch(double[] a, double key)  
7. static int binarySearch(double[] a, int fromIndex, int toIndex, double key)  
8. static int binarySearch(float[] a, float key)  
9. static int binarySearch(float[] a, int fromIndex, int toIndex, float key)  
10. static int binarySearch(int[] a, int key)  
11. static int binarySearch(int[] a, int fromIndex, int toIndex, int key)  
12. static int binarySearch(long[] a, int fromIndex, int toIndex, long key)  
13. static int binarySearch(long[] a, long key)  
14. static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)  
15. static int binarySearch(Object[] a, Object key)  
16. static int binarySearch(short[] a, int fromIndex, int toIndex, short key)  
17. static int binarySearch(short[] a, short key)  
18. static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) 	 :在制定范围内搜索给定类中的特定对象。注意:要实现Comparator接口尖括号中可以是<T的父类>。下注!
19. static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) 
20. static boolean[] copyOf(boolean[] original, int newLength)	:复制指定的数组,截断或用false填充(如果需要),以使副本具有指定的长度。
21. static byte[] copyOf(byte[] original, int newLength)		:用0填充
22. static char[] copyOf(char[] original, int newLength) 		:用null填充
23. static double[] copyOf(double[] original, int newLength)  
24. static float[] copyOf(float[] original, int newLength)  
25. static int[] copyOf(int[] original, int newLength)  
26. static long[] copyOf(long[] original, int newLength)  
27. static short[] copyOf(short[] original, int newLength)  
28. static <T> T[] copyOf(T[] original, int newLength) 			:用null填充
29. static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) 	:复制指定的数组,截断或用null填充(如果需要),以使副本具有指定的长度;在复制过程中转型。下注!
30. static boolean[] copyOfRange(boolean[] original, int from, int to)  	:将指定数组的指定范围复制到新的数组中。
31. static byte[] copyOfRange(byte[] original, int from, int to)  
32. static char[] copyOfRange(char[] original, int from, int to)  
33. static double[] copyOfRange(double[] original, int from, int to)  
34. static float[] copyOfRange(float[] original, int from, int to)  
35. static int[] copyOfRange(int[] original, int from, int to)  
36. static long[] copyOfRange(long[] original, int from, int to)   
37. static short[] copyOfRange(short[] original, int from, int to)  
38. static <T> T[] copyOfRange(T[] original, int from, int to)  
39. static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)  
40. static boolean deepEquals(Object[] a1, Object[] a2)  	:比较两个数组是否相等,但是必须是类类型,不能是基础类型。
41. static int deepHashCode(Object[] a)  	:和hashCode作用一样,返回这个数组的 hashCode。用作多维数组。
42. static String deepToString(Object[] a)  :效果个toString一样。用作多维数组。
43. static boolean equals(boolean[] a, boolean[] a2)  
44. static boolean equals(byte[] a, byte[] a2)  
45. static boolean equals(char[] a, char[] a2)  
46. static boolean equals(double[] a, double[] a2)  
47. static boolean equals(float[] a, float[] a2)  
48. static boolean equals(int[] a, int[] a2)  
49. static boolean equals(long[] a, long[] a2)  
50. static boolean equals(Object[] a, Object[] a2)  
51. static boolean equals(short[] a, short[] a2)  
52. static void fill(boolean[] a, boolean val) 		:将val的值赋给a的每一个元素。
53. static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)  	:将val的值赋给a的指定范围内的,每一个元素。包含fromIndex,不包含toIndex.
54. static void fill(byte[] a, byte val)  
55. static void fill(byte[] a, int fromIndex, int toIndex, byte val) 
56. static void fill(char[] a, char val)  
57. static void fill(char[] a, int fromIndex, int toIndex, char val)  
58. static void fill(double[] a, double val)  
59. static void fill(double[] a, int fromIndex, int toIndex, double val)  
60. static void fill(float[] a, float val)  
61. static void fill(float[] a, int fromIndex, int toIndex, float val)  
62. static void fill(int[] a, int val)  
63. static void fill(int[] a, int fromIndex, int toIndex, int val)  
64. static void fill(long[] a, int fromIndex, int toIndex, long val)  
65. static void fill(long[] a, long val)  
66. static void fill(Object[] a, int fromIndex, int toIndex, Object val)  
67. static void fill(Object[] a, Object val)  
68. static void fill(short[] a, int fromIndex, int toIndex, short val)  
69. static void fill(short[] a, short val)  
70. static int hashCode(boolean[] a)  
71. static int hashCode(byte[] a)  
72. static int hashCode(char[] a)  
73. static int hashCode(double[] a)  
74. static int hashCode(float[] a)  
75. static int hashCode(int[] a)  
76. static int hashCode(long[] a)  
77. static int hashCode(Object[] a)  
78. static int hashCode(short[] a)  
79. static void parallelPrefix(double[] array, DoubleBinaryOperator op) 	:使用提供的函数并行地运算给定数组的每个元素。下注!
80. static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)  
81. static void parallelPrefix(int[] array, IntBinaryOperator op)  
82. static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)  
83. static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)  
84. static void parallelPrefix(long[] array, LongBinaryOperator op)  
85. static <T> void parallelPrefix(T[] array, BinaryOperator<T> op)  
86. static <T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)  
87. static void parallelSetAll(double[] array, IntToDoubleFunction generator)  		:使用提供的生成器函数并行地设置指定数组的所有元素。下注!
88. static void parallelSetAll(int[] array, IntUnaryOperator generator)  
89. static void parallelSetAll(long[] array, IntToLongFunction generator)  
90. static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator)  
91. static void parallelSort(byte[] a)  	:对数组a并行地进行排序(升序)。
92. static void parallelSort(byte[] a, int fromIndex, int toIndex)  
93. static void parallelSort(char[] a)  
94. static void parallelSort(char[] a, int fromIndex, int toIndex)  
95. static void parallelSort(double[] a)  
96. static void parallelSort(double[] a, int fromIndex, int toIndex)  
97. static void parallelSort(float[] a)  
98. static void parallelSort(float[] a, int fromIndex, int toIndex)  
99. static void parallelSort(int[] a)  
100. static void parallelSort(int[] a, int fromIndex, int toIndex)  
101. static void parallelSort(long[] a)  
102. static void parallelSort(long[] a, int fromIndex, int toIndex)  
103. static void parallelSort(short[] a)  
104. static void parallelSort(short[] a, int fromIndex, int toIndex)  
105. static <T extends Comparable<? super T>> void parallelSort(T[] a)  	:类型T需要实现Comparable接口,或它的父类实现Comparable接口。下注!
106. static <T> void parallelSort(T[] a, Comparator<? super T> cmp)  
107. static <T extends Comparable<? super T>> void parallelSort(T[] a, int fromIndex, int toIndex)  
108. static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)  
109. static void setAll(double[] array, IntToDoubleFunction generator)  
110. static void setAll(int[] array, IntUnaryOperator generator)  
111. static void setAll(long[] array, IntToLongFunction generator)  
112. static <T> void setAll(T[] array, IntFunction<? extends T> generator)  
113. static void sort(byte[] a)  
114. static void sort(byte[] a, int fromIndex, int toIndex)  
115. static void sort(char[] a)  
116. static void sort(char[] a, int fromIndex, int toIndex)  
117. static void sort(double[] a)  
118. static void sort(double[] a, int fromIndex, int toIndex)  
119. static void sort(float[] a)  
120. static void sort(float[] a, int fromIndex, int toIndex)  
121. static void sort(int[] a)  
122. static void sort(int[] a, int fromIndex, int toIndex)  
123. static void sort(long[] a)  
124. static void sort(long[] a, int fromIndex, int toIndex)  
125. static void sort(Object[] a)  
126. static void sort(Object[] a, int fromIndex, int toIndex)  
127. static void sort(short[] a)  
128. static void sort(short[] a, int fromIndex, int toIndex)  
129. static <T> void sort(T[] a, Comparator<? super T> c)  
130. static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)  
131. static Spliterator.OfDouble spliterator(double[] array)  	:简单来说就是多线程的一个迭代器,函数讲解请看这篇文章(https://blog.csdn.net/QiwooMobile/article/details/85992301132. static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive)  
133. static Spliterator.OfInt spliterator(int[] array)  
134. static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive)  
135. static Spliterator.OfLong spliterator(long[] array)  
136. static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive)  
137. static <T> Spliterator<T> spliterator(T[] array)  
138. static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive)  
139. static DoubleStream stream(double[] array)  
140. static DoubleStream stream(double[] array, int startInclusive, int endExclusive)  
141. static IntStream stream(int[] array)  		:将数组转化成流进行操作。返回值是Stream的子类,Stream是8的新特性,是对collection的操作,非常好用,建议找Stream的用法来看看,这里就不介绍了。
142. static IntStream stream(int[] array, int startInclusive, int endExclusive)  
143. static LongStream stream(long[] array)  
144. static LongStream stream(long[] array, int startInclusive, int endExclusive)  
145. static <T> Stream<T> stream(T[] array)  
146. static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive)  
147. static String toString(boolean[] a)  
148. static String toString(byte[] a)  
149. static String toString(char[] a)  
150. static String toString(double[] a)  
151. static String toString(float[] a)  
152. static String toString(int[] a)  
153. static String toString(long[] a)  
154. static String toString(Object[] a)  
155. static String toString(short[] a)  

注:第18个函数binarySearch

import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        Dog d1 = new Dog(1,2);
        Dog d2 = new Dog(2,3);
        Dog d3 = new Dog(3,4);
        Dog[] dogs = {d3,d2,d1};
        int a = Arrays.binarySearch(dogs, d2, new ComparableSize());
        System.out.println(a);
    }
}
class Animal{
    public int size;

    public Animal(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "size=" + size +
                '}';
    }
}
class Dog extends Animal{
    public int wight;

    public Dog(int size, int wight) {
        super(size);
        this.wight = wight;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "size=" + size +
                ", wight=" + wight +
                '}';
    }
}
class ComparableSize implements Comparator<Animal> {
    @Override
    public int compare(Animal d1,Animal d2) {
        return d1.size - d2.size;
    }

}

注:第29个函数copyOf

public static void main(String[] args) {
        Number[] arr = {1,2,3};
        Integer[] arr1 = Arrays.copyOf(arr, 5, Integer[].class);
        //Integer[] arr2 = (Integer[]) arr;               //可以发现copyOf函数的用处
        for (int i = 0; i < arr1.length; i++){
            System.out.print(arr2[i]+" ");
        }

    }

equals和deepEquals的区别:

先看原码:

public static boolean equals(Object[] a, Object[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

        int length = a.length;
        if (a2.length != length)
            return false;

        for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }

        return true;
    }
public static boolean deepEquals(Object[] a1, Object[] a2) {
        if (a1 == a2)
            return true;
        if (a1 == null || a2==null)
            return false;
        int length = a1.length;
        if (a2.length != length)
            return false;

        for (int i = 0; i < length; i++) {
            Object e1 = a1[i];
            Object e2 = a2[i];

            if (e1 == e2)
                continue;
            if (e1 == null)
                return false;

            // Figure out whether the two elements are equal
            boolean eq = deepEquals0(e1, e2);

            if (!eq)
                return false;
        }
        return true;
    }

简要言之,deepEquals可以判断多维数组,equals不能判断多维数组。
实例:

public static void main(String[] args) {
        Integer[][][] arr1 = {{{1,2},{3,4}},{{5,null}}};
        Integer[][][] arr2 = {{{1,2},{3,4}},{{5,null}}};
        System.out.println(Arrays.deepEquals(arr1,arr2));
        System.out.println(Arrays.equals(arr1,arr2));
    }




    /*
    	输出结果:
    	true
		false
	*/

parallelPrefix函数实例

public static void main(String[] args) {
        int[] arr = {1,3,23,4,53,45};
        Arrays.parallelPrefix(arr, new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                return left+right;
            }
        });
        System.out.println(Arrays.toString(arr));
    }


	/*
    	输出结果:
    	[1, 4, 27, 31, 84, 129]
	*/

parallelSetAll函数实例

是对下标进行操作

public static void main(String[] args) {
        int[] arr = {2,3,23,4,53,45};
        Arrays.parallelSetAll(arr, new IntUnaryOperator() {
            @Override
            public int applyAsInt(int operand) {
                return operand+3;
            }
        });
    }


	/*
    	输出结果:
    	[3, 4, 5, 6, 7, 8]
	*/

通过parallelSort函数和Sort函数简单对比并行的执行效率

public static void main(String[] args) {
        int[] arr = {2,3,23,4,53,45,123,4324,2,13,35,234,123,4325,56457,57885,234523};
        long startTime = System.nanoTime();    //获取开始时间

        Arrays.sort(arr);

        long endTime = System.nanoTime();    //获取结束时间

        System.out.println("程序运行时间:" + (endTime - startTime) + "ns");    //输出程序运行时间
        long startTime1 = System.nanoTime();    //获取开始时间

        Arrays.parallelSort(arr);

        long endTime1 = System.nanoTime();    //获取结束时间

        System.out.println("程序并行运行时间:" + (endTime1 - startTime1) + "ns");    //输出程序运行时间
    }




	/*
    	输出结果:
    	程序运行时间:340800ms
		程序并行运行时间:9600ms
	*/

关于第105个函数 Comparable

public static void main(String[] args) {
        System.out.println(Arrays.toString(arr));
        Dog[] arr1 = {new Dog(3,0),new Dog(12,0),new Dog(1,0),new Dog(1423,0),new Dog(101,0)};
        Arrays.parallelSort(arr1);
        for (int i = 0; i < arr1.length; i++){
            System.out.println(arr1[i]);
        }
    }
}
class Animal  implements Comparable<Animal>{
    public int size;

    public Animal(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "size=" + size +
                '}';
    }
    @Override
    public int compareTo(Animal o) {
        return this.size - o.size;
    }
}
class Dog extends Animal{
    public int wight;

    public Dog(int size, int wight) {
        super(size);
        this.wight = wight;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "size=" + size +
                ", wight=" + wight +
                '}';
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脸是真的白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值