当前位置:   article > 正文

深入学习java源码之Arrays.binarySearch()与Arrays.copyOf()_method arrays.binarysearch(byte[],int,int,byte) is

method arrays.binarysearch(byte[],int,int,byte) is not applicable (argument

深入学习java源码之Arrays.binarySearch()与Arrays.copyOf()

java中有三种移位运算

<<      :     左移运算符,num << 1,相当于num乘以2

>>      :     右移运算符,num >> 1,相当于num除以2

>>>    :     无符号右移,忽略符号位,空位都以0补齐

算术左移和算术右移主要用来进行有符号数的倍增、减半 
逻辑左移和逻辑右移主要用来进行无符号数的倍增、减半 

比如一个有符号位的8位二进制数10101010,[]是添加的数字

逻辑左移一位:0101010[0] 
逻辑左移两位:101010[00]

算术左移一位:0101010[0] 
算术左移两位:101010[00]

逻辑右移一位:[0]1010101 
逻辑右移两位:[00]101010

算术右移一位:[1]1010101 
算术右移两位:[11]101010
 

  1. // 20
  2. System.out.println(10 << 1);
  3. // -20
  4. System.out.println(-10 << 1);
  5. // 5
  6. System.out.println(10 >> 1);
  7. // -5
  8. System.out.println(-10 >> 1);
  9. // 5
  10. System.out.println(10 >>> 1);
  11. // 2147483643
  12. System.out.println(-10 >>> 1);

论java中System.arrayCopy()与Arrays.copyOf()的区别

如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式。

System.arraycopy()

  1. int[] arr = {1,2,3,4,5};
  2. int[] copied=new int[10];
  3. System.arraycopy(arr,0,copied,1,5);//这里的arr是原数组,0是原数组拷贝的其实地址。而copied是目标数组,1是目标数组开始存放的位置,5则是数组存放的长度。
  4. System.out.println(Array.toString(copied));
  5. 运行结果如下:
  6. [0,1,2,3,4,5,0,0,0,0];

arrays.copyof()

  1. int[] arr = {1,2,3,4,5};
  2. int copied=arrays.copyof(arr,10);
  3. System.out.println(Arrays.toString(copied));
  4. copied=arrays.copyof(arr,3);
  5. System.out.println(Arrays.toString(copied));
  6. 运行结果如下:
  7. [1,2,3,4,5,0,0,0,0,0]
  8. [1,2,3]

这里我们来谈谈他们的区别,这也是之前我一直没弄懂的地方:

两者的区别在于,Arrays.copyOf()不仅仅只是拷贝数组中的元素,在拷贝元素时,会创建一个新的数组对象。而System.arrayCopy只拷贝已经存在数组元素。

如果我们看过Arrays.copyOf()的源码就会知道,该方法的底层还是调用了System.arrayCopyOf()方法。

而且System.arrayCopy如果改变目标数组的值原数组的值也会随之改变。

 

Modifier and TypeMethod and Description
static intbinarySearch(byte[] a, byte key)

使用二进制搜索算法搜索指定值的指定字节数组。

static intbinarySearch(byte[] a, int fromIndex, int toIndex, byte key)

使用二进制搜索算法搜索指定值的指定字节数组的范围。

static intbinarySearch(char[] a, char key)

使用二进制搜索算法搜索指定数组的指定值。

static intbinarySearch(char[] a, int fromIndex, int toIndex, char key)

使用二分搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(double[] a, double key)

使用二进制搜索算法搜索指定值的指定数组的双精度值。

static intbinarySearch(double[] a, int fromIndex, int toIndex, double key)

使用二分搜索算法搜索指定值的指定数组的双精度范围。

static intbinarySearch(float[] a, float key)

使用二叉搜索算法搜索指定数组的浮点数。

static intbinarySearch(float[] a, int fromIndex, int toIndex, float key)

使用二分搜索算法搜索指定数组的浮点数范围。

static intbinarySearch(int[] a, int key)

使用二叉搜索算法搜索指定的int数组的指定值。

static intbinarySearch(int[] a, int fromIndex, int toIndex, int key)

使用二叉搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(long[] a, int fromIndex, int toIndex, long key)

使用二分搜索算法搜索指定值的指定数组的范围。

static intbinarySearch(long[] a, long key)

使用二进制搜索算法搜索指定数组的指定数组。

static intbinarySearch(Object[] a, int fromIndex, int toIndex, Object key)

使用二进制搜索算法搜索指定对象的指定数组的范围。

static intbinarySearch(Object[] a, Object key)

使用二叉搜索算法搜索指定对象的指定数组。

static intbinarySearch(short[] a, int fromIndex, int toIndex, short key)

使用二进制搜索算法搜索指定值的指定数组的短整型范围。

static intbinarySearch(short[] a, short key)

使用二进制搜索算法搜索指定值的指定数组的指定值。

static <T> intbinarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)

使用二进制搜索算法搜索指定对象的指定数组的范围。

static <T> intbinarySearch(T[] a, T key, Comparator<? super T> c)

使用二叉搜索算法搜索指定对象的指定数组。

static boolean[]copyOf(boolean[] original, int newLength)

使用 false (如有必要)复制指定的数组,截断或填充,以使副本具有指定的长度。

static byte[]copyOf(byte[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static char[]copyOf(char[] original, int newLength)

复制指定的数组,截断或填充空字符(如有必要),以便复制具有指定的长度。

static double[]copyOf(double[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static float[]copyOf(float[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static int[]copyOf(int[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static long[]copyOf(long[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static short[]copyOf(short[] original, int newLength)

复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。

static <T> T[]copyOf(T[] original, int newLength)

复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。

static <T,U> T[]copyOf(U[] original, int newLength, <? extends T[]> newType)

复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。

static boolean[]copyOfRange(boolean[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static byte[]copyOfRange(byte[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static char[]copyOfRange(char[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static double[]copyOfRange(double[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static float[]copyOfRange(float[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static int[]copyOfRange(int[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static long[]copyOfRange(long[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static short[]copyOfRange(short[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static <T> T[]copyOfRange(T[] original, int from, int to)

将指定数组的指定范围复制到新数组中。

static <T,U> T[]copyOfRange(U[] original, int from, int to, <? extends T[]> newType)

将指定数组的指定范围复制到新数组中。

static booleandeepEquals(Object[] a1, Object[] a2)

如果两个指定的数组彼此 深度相等 ,则返回 true 。

static intdeepHashCode(Object[] a)

根据指定数组的“深度内容”返回哈希码。

static StringdeepToString(Object[] a)

返回指定数组的“深度内容”的字符串表示形式。

static booleanequals(boolean[] a, boolean[] a2)

如果两个指定的布尔数组彼此 相等 ,则返回 true 。

static booleanequals(byte[] a, byte[] a2)

如果两个指定的字节数组彼此 相等 ,则返回 true 。

static booleanequals(char[] a, char[] a2)

如果两个指定的字符数组彼此 相等 ,则返回 true 。

static booleanequals(double[] a, double[] a2)

如果两个指定的双精度数组彼此 相等 ,则返回 true 。

static booleanequals(float[] a, float[] a2)

如果两个指定的浮动数组彼此 相等 ,则返回 true 。

static booleanequals(int[] a, int[] a2)

如果两个指定的int数组彼此 相等 ,则返回 true 。

static booleanequals(long[] a, long[] a2)

如果两个指定的longs数组彼此 相等 ,则返回 true 。

static booleanequals(Object[] a, Object[] a2)

如果两个指定的对象数组彼此 相等 ,则返回 true 。

static booleanequals(short[] a, short[] a2)

如果两个指定的短裤阵列彼此 相等 ,则返回 true 。

static voidfill(boolean[] a, boolean val)

将指定的布尔值分配给指定的布尔数组的每个元素。

static voidfill(boolean[] a, int fromIndex, int toIndex, boolean val)

将指定的布尔值分配给指定数组布尔值的指定范围的每个元素。

static voidfill(byte[] a, byte val)

将指定的字节值分配给指定字节数组的每个元素。

static voidfill(byte[] a, int fromIndex, int toIndex, byte val)

将指定的字节值分配给指定字节数组的指定范围的每个元素。

static voidfill(char[] a, char val)

将指定的char值分配给指定的char数组的每个元素。

static voidfill(char[] a, int fromIndex, int toIndex, char val)

将指定的char值分配给指定的char数组的指定范围的每个元素。

static voidfill(double[] a, double val)

将指定的double值分配给指定的双精度数组的每个元素。

static voidfill(double[] a, int fromIndex, int toIndex, double val)

将指定的double值分配给指定的双精度数组范围的每个元素。

static voidfill(float[] a, float val)

将指定的float值分配给指定的浮点数组的每个元素。

static voidfill(float[] a, int fromIndex, int toIndex, float val)

将指定的浮点值分配给指定的浮点数组的指定范围的每个元素。

static voidfill(int[] a, int val)

将指定的int值分配给指定的int数组的每个元素。

static voidfill(int[] a, int fromIndex, int toIndex, int val)

将指定的int值分配给指定的int数组的指定范围的每个元素。

static voidfill(long[] a, int fromIndex, int toIndex, long val)

将指定的long值分配给指定的longs数组的指定范围的每个元素。

static voidfill(long[] a, long val)

将指定的long值分配给指定的longs数组的每个元素。

static voidfill(Object[] a, int fromIndex, int toIndex, Object val)

将指定的对象引用分配给指定的对象数组的指定范围的每个元素。

static voidfill(Object[] a, Object val)

将指定的对象引用分配给指定的对象数组的每个元素。

static voidfill(short[] a, int fromIndex, int toIndex, short val)

将指定的短值分配给指定的短裤数组的指定范围的每个元素。

static voidfill(short[] a, short val)

将指定的短值分配给指定的短裤数组的每个元素。

java源码

  1. package java.util;
  2. import java.lang.reflect.Array;
  3. import java.util.concurrent.ForkJoinPool;
  4. import java.util.function.BinaryOperator;
  5. import java.util.function.Consumer;
  6. import java.util.function.DoubleBinaryOperator;
  7. import java.util.function.IntBinaryOperator;
  8. import java.util.function.IntFunction;
  9. import java.util.function.IntToDoubleFunction;
  10. import java.util.function.IntToLongFunction;
  11. import java.util.function.IntUnaryOperator;
  12. import java.util.function.LongBinaryOperator;
  13. import java.util.function.UnaryOperator;
  14. import java.util.stream.DoubleStream;
  15. import java.util.stream.IntStream;
  16. import java.util.stream.LongStream;
  17. import java.util.stream.Stream;
  18. import java.util.stream.StreamSupport;
  19. public class Arrays {
  20. private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
  21. // Suppresses default constructor, ensuring non-instantiability.
  22. private Arrays() {}
  23. static final class NaturalOrder implements Comparator<Object> {
  24. @SuppressWarnings("unchecked")
  25. public int compare(Object first, Object second) {
  26. return ((Comparable<Object>)first).compareTo(second);
  27. }
  28. static final NaturalOrder INSTANCE = new NaturalOrder();
  29. }
  30. private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
  31. if (fromIndex > toIndex) {
  32. throw new IllegalArgumentException(
  33. "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
  34. }
  35. if (fromIndex < 0) {
  36. throw new ArrayIndexOutOfBoundsException(fromIndex);
  37. }
  38. if (toIndex > arrayLength) {
  39. throw new ArrayIndexOutOfBoundsException(toIndex);
  40. }
  41. }
  42. public static int binarySearch(long[] a, long key) {
  43. return binarySearch0(a, 0, a.length, key);
  44. }
  45. public static int binarySearch(long[] a, int fromIndex, int toIndex,
  46. long key) {
  47. rangeCheck(a.length, fromIndex, toIndex);
  48. return binarySearch0(a, fromIndex, toIndex, key);
  49. }
  50. // Like public version, but without range checks.
  51. private static int binarySearch0(long[] a, int fromIndex, int toIndex,
  52. long key) {
  53. int low = fromIndex;
  54. int high = toIndex - 1;
  55. while (low <= high) {
  56. int mid = (low + high) >>> 1;
  57. long midVal = a[mid];
  58. if (midVal < key)
  59. low = mid + 1;
  60. else if (midVal > key)
  61. high = mid - 1;
  62. else
  63. return mid; // key found
  64. }
  65. return -(low + 1); // key not found.
  66. }
  67. public static int binarySearch(int[] a, int key) {
  68. return binarySearch0(a, 0, a.length, key);
  69. }
  70. public static int binarySearch(int[] a, int fromIndex, int toIndex,
  71. int key) {
  72. rangeCheck(a.length, fromIndex, toIndex);
  73. return binarySearch0(a, fromIndex, toIndex, key);
  74. }
  75. // Like public version, but without range checks.
  76. private static int binarySearch0(int[] a, int fromIndex, int toIndex,
  77. int key) {
  78. int low = fromIndex;
  79. int high = toIndex - 1;
  80. while (low <= high) {
  81. int mid = (low + high) >>> 1;
  82. int midVal = a[mid];
  83. if (midVal < key)
  84. low = mid + 1;
  85. else if (midVal > key)
  86. high = mid - 1;
  87. else
  88. return mid; // key found
  89. }
  90. return -(low + 1); // key not found.
  91. }
  92. public static int binarySearch(short[] a, short key) {
  93. return binarySearch0(a, 0, a.length, key);
  94. }
  95. public static int binarySearch(short[] a, int fromIndex, int toIndex,
  96. short key) {
  97. rangeCheck(a.length, fromIndex, toIndex);
  98. return binarySearch0(a, fromIndex, toIndex, key);
  99. }
  100. // Like public version, but without range checks.
  101. private static int binarySearch0(short[] a, int fromIndex, int toIndex,
  102. short key) {
  103. int low = fromIndex;
  104. int high = toIndex - 1;
  105. while (low <= high) {
  106. int mid = (low + high) >>> 1;
  107. short midVal = a[mid];
  108. if (midVal < key)
  109. low = mid + 1;
  110. else if (midVal > key)
  111. high = mid - 1;
  112. else
  113. return mid; // key found
  114. }
  115. return -(low + 1); // key not found.
  116. }
  117. public static int binarySearch(char[] a, char key) {
  118. return binarySearch0(a, 0, a.length, key);
  119. }
  120. public static int binarySearch(char[] a, int fromIndex, int toIndex,
  121. char key) {
  122. rangeCheck(a.length, fromIndex, toIndex);
  123. return binarySearch0(a, fromIndex, toIndex, key);
  124. }
  125. // Like public version, but without range checks.
  126. private static int binarySearch0(char[] a, int fromIndex, int toIndex,
  127. char key) {
  128. int low = fromIndex;
  129. int high = toIndex - 1;
  130. while (low <= high) {
  131. int mid = (low + high) >>> 1;
  132. char midVal = a[mid];
  133. if (midVal < key)
  134. low = mid + 1;
  135. else if (midVal > key)
  136. high = mid - 1;
  137. else
  138. return mid; // key found
  139. }
  140. return -(low + 1); // key not found.
  141. }
  142. public static int binarySearch(byte[] a, byte key) {
  143. return binarySearch0(a, 0, a.length, key);
  144. }
  145. public static int binarySearch(byte[] a, int fromIndex, int toIndex,
  146. byte key) {
  147. rangeCheck(a.length, fromIndex, toIndex);
  148. return binarySearch0(a, fromIndex, toIndex, key);
  149. }
  150. // Like public version, but without range checks.
  151. private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
  152. byte key) {
  153. int low = fromIndex;
  154. int high = toIndex - 1;
  155. while (low <= high) {
  156. int mid = (low + high) >>> 1;
  157. byte midVal = a[mid];
  158. if (midVal < key)
  159. low = mid + 1;
  160. else if (midVal > key)
  161. high = mid - 1;
  162. else
  163. return mid; // key found
  164. }
  165. return -(low + 1); // key not found.
  166. }
  167. public static int binarySearch(double[] a, double key) {
  168. return binarySearch0(a, 0, a.length, key);
  169. }
  170. public static int binarySearch(double[] a, int fromIndex, int toIndex,
  171. double key) {
  172. rangeCheck(a.length, fromIndex, toIndex);
  173. return binarySearch0(a, fromIndex, toIndex, key);
  174. }
  175. // Like public version, but without range checks.
  176. private static int binarySearch0(double[] a, int fromIndex, int toIndex,
  177. double key) {
  178. int low = fromIndex;
  179. int high = toIndex - 1;
  180. while (low <= high) {
  181. int mid = (low + high) >>> 1;
  182. double midVal = a[mid];
  183. if (midVal < key)
  184. low = mid + 1; // Neither val is NaN, thisVal is smaller
  185. else if (midVal > key)
  186. high = mid - 1; // Neither val is NaN, thisVal is larger
  187. else {
  188. long midBits = Double.doubleToLongBits(midVal);
  189. long keyBits = Double.doubleToLongBits(key);
  190. if (midBits == keyBits) // Values are equal
  191. return mid; // Key found
  192. else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
  193. low = mid + 1;
  194. else // (0.0, -0.0) or (NaN, !NaN)
  195. high = mid - 1;
  196. }
  197. }
  198. return -(low + 1); // key not found.
  199. }
  200. public static int binarySearch(float[] a, float key) {
  201. return binarySearch0(a, 0, a.length, key);
  202. }
  203. public static int binarySearch(float[] a, int fromIndex, int toIndex,
  204. float key) {
  205. rangeCheck(a.length, fromIndex, toIndex);
  206. return binarySearch0(a, fromIndex, toIndex, key);
  207. }
  208. // Like public version, but without range checks.
  209. private static int binarySearch0(float[] a, int fromIndex, int toIndex,
  210. float key) {
  211. int low = fromIndex;
  212. int high = toIndex - 1;
  213. while (low <= high) {
  214. int mid = (low + high) >>> 1;
  215. float midVal = a[mid];
  216. if (midVal < key)
  217. low = mid + 1; // Neither val is NaN, thisVal is smaller
  218. else if (midVal > key)
  219. high = mid - 1; // Neither val is NaN, thisVal is larger
  220. else {
  221. int midBits = Float.floatToIntBits(midVal);
  222. int keyBits = Float.floatToIntBits(key);
  223. if (midBits == keyBits) // Values are equal
  224. return mid; // Key found
  225. else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
  226. low = mid + 1;
  227. else // (0.0, -0.0) or (NaN, !NaN)
  228. high = mid - 1;
  229. }
  230. }
  231. return -(low + 1); // key not found.
  232. }
  233. public static int binarySearch(Object[] a, Object key) {
  234. return binarySearch0(a, 0, a.length, key);
  235. }
  236. public static int binarySearch(Object[] a, int fromIndex, int toIndex,
  237. Object key) {
  238. rangeCheck(a.length, fromIndex, toIndex);
  239. return binarySearch0(a, fromIndex, toIndex, key);
  240. }
  241. // Like public version, but without range checks.
  242. private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
  243. Object key) {
  244. int low = fromIndex;
  245. int high = toIndex - 1;
  246. while (low <= high) {
  247. int mid = (low + high) >>> 1;
  248. @SuppressWarnings("rawtypes")
  249. Comparable midVal = (Comparable)a[mid];
  250. @SuppressWarnings("unchecked")
  251. int cmp = midVal.compareTo(key);
  252. if (cmp < 0)
  253. low = mid + 1;
  254. else if (cmp > 0)
  255. high = mid - 1;
  256. else
  257. return mid; // key found
  258. }
  259. return -(low + 1); // key not found.
  260. }
  261. public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
  262. return binarySearch0(a, 0, a.length, key, c);
  263. }
  264. public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
  265. T key, Comparator<? super T> c) {
  266. rangeCheck(a.length, fromIndex, toIndex);
  267. return binarySearch0(a, fromIndex, toIndex, key, c);
  268. }
  269. // Like public version, but without range checks.
  270. private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
  271. T key, Comparator<? super T> c) {
  272. if (c == null) {
  273. return binarySearch0(a, fromIndex, toIndex, key);
  274. }
  275. int low = fromIndex;
  276. int high = toIndex - 1;
  277. while (low <= high) {
  278. int mid = (low + high) >>> 1;
  279. T midVal = a[mid];
  280. int cmp = c.compare(midVal, key);
  281. if (cmp < 0)
  282. low = mid + 1;
  283. else if (cmp > 0)
  284. high = mid - 1;
  285. else
  286. return mid; // key found
  287. }
  288. return -(low + 1); // key not found.
  289. }
  290. public static boolean equals(long[] a, long[] a2) {
  291. if (a==a2)
  292. return true;
  293. if (a==null || a2==null)
  294. return false;
  295. int length = a.length;
  296. if (a2.length != length)
  297. return false;
  298. for (int i=0; i<length; i++)
  299. if (a[i] != a2[i])
  300. return false;
  301. return true;
  302. }
  303. public static boolean equals(int[] a, int[] a2) {
  304. if (a==a2)
  305. return true;
  306. if (a==null || a2==null)
  307. return false;
  308. int length = a.length;
  309. if (a2.length != length)
  310. return false;
  311. for (int i=0; i<length; i++)
  312. if (a[i] != a2[i])
  313. return false;
  314. return true;
  315. }
  316. public static boolean equals(short[] a, short a2[]) {
  317. if (a==a2)
  318. return true;
  319. if (a==null || a2==null)
  320. return false;
  321. int length = a.length;
  322. if (a2.length != length)
  323. return false;
  324. for (int i=0; i<length; i++)
  325. if (a[i] != a2[i])
  326. return false;
  327. return true;
  328. }
  329. public static boolean equals(char[] a, char[] a2) {
  330. if (a==a2)
  331. return true;
  332. if (a==null || a2==null)
  333. return false;
  334. int length = a.length;
  335. if (a2.length != length)
  336. return false;
  337. for (int i=0; i<length; i++)
  338. if (a[i] != a2[i])
  339. return false;
  340. return true;
  341. }
  342. public static boolean equals(byte[] a, byte[] a2) {
  343. if (a==a2)
  344. return true;
  345. if (a==null || a2==null)
  346. return false;
  347. int length = a.length;
  348. if (a2.length != length)
  349. return false;
  350. for (int i=0; i<length; i++)
  351. if (a[i] != a2[i])
  352. return false;
  353. return true;
  354. }
  355. public static boolean equals(boolean[] a, boolean[] a2) {
  356. if (a==a2)
  357. return true;
  358. if (a==null || a2==null)
  359. return false;
  360. int length = a.length;
  361. if (a2.length != length)
  362. return false;
  363. for (int i=0; i<length; i++)
  364. if (a[i] != a2[i])
  365. return false;
  366. return true;
  367. }
  368. public static boolean equals(double[] a, double[] a2) {
  369. if (a==a2)
  370. return true;
  371. if (a==null || a2==null)
  372. return false;
  373. int length = a.length;
  374. if (a2.length != length)
  375. return false;
  376. for (int i=0; i<length; i++)
  377. if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
  378. return false;
  379. return true;
  380. }
  381. public static boolean equals(float[] a, float[] a2) {
  382. if (a==a2)
  383. return true;
  384. if (a==null || a2==null)
  385. return false;
  386. int length = a.length;
  387. if (a2.length != length)
  388. return false;
  389. for (int i=0; i<length; i++)
  390. if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
  391. return false;
  392. return true;
  393. }
  394. public static boolean equals(Object[] a, Object[] a2) {
  395. if (a==a2)
  396. return true;
  397. if (a==null || a2==null)
  398. return false;
  399. int length = a.length;
  400. if (a2.length != length)
  401. return false;
  402. for (int i=0; i<length; i++) {
  403. Object o1 = a[i];
  404. Object o2 = a2[i];
  405. if (!(o1==null ? o2==null : o1.equals(o2)))
  406. return false;
  407. }
  408. return true;
  409. }
  410. public static void fill(long[] a, long val) {
  411. for (int i = 0, len = a.length; i < len; i++)
  412. a[i] = val;
  413. }
  414. public static void fill(long[] a, int fromIndex, int toIndex, long val) {
  415. rangeCheck(a.length, fromIndex, toIndex);
  416. for (int i = fromIndex; i < toIndex; i++)
  417. a[i] = val;
  418. }
  419. public static void fill(int[] a, int val) {
  420. for (int i = 0, len = a.length; i < len; i++)
  421. a[i] = val;
  422. }
  423. public static void fill(int[] a, int fromIndex, int toIndex, int val) {
  424. rangeCheck(a.length, fromIndex, toIndex);
  425. for (int i = fromIndex; i < toIndex; i++)
  426. a[i] = val;
  427. }
  428. public static void fill(short[] a, short val) {
  429. for (int i = 0, len = a.length; i < len; i++)
  430. a[i] = val;
  431. }
  432. public static void fill(short[] a, int fromIndex, int toIndex, short val) {
  433. rangeCheck(a.length, fromIndex, toIndex);
  434. for (int i = fromIndex; i < toIndex; i++)
  435. a[i] = val;
  436. }
  437. public static void fill(char[] a, int fromIndex, int toIndex, char val) {
  438. rangeCheck(a.length, fromIndex, toIndex);
  439. for (int i = fromIndex; i < toIndex; i++)
  440. a[i] = val;
  441. }
  442. public static void fill(byte[] a, byte val) {
  443. for (int i = 0, len = a.length; i < len; i++)
  444. a[i] = val;
  445. }
  446. public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
  447. rangeCheck(a.length, fromIndex, toIndex);
  448. for (int i = fromIndex; i < toIndex; i++)
  449. a[i] = val;
  450. }
  451. public static void fill(boolean[] a, boolean val) {
  452. for (int i = 0, len = a.length; i < len; i++)
  453. a[i] = val;
  454. }
  455. public static void fill(boolean[] a, int fromIndex, int toIndex,
  456. boolean val) {
  457. rangeCheck(a.length, fromIndex, toIndex);
  458. for (int i = fromIndex; i < toIndex; i++)
  459. a[i] = val;
  460. }
  461. public static void fill(double[] a, double val) {
  462. for (int i = 0, len = a.length; i < len; i++)
  463. a[i] = val;
  464. }
  465. public static void fill(double[] a, int fromIndex, int toIndex,double val){
  466. rangeCheck(a.length, fromIndex, toIndex);
  467. for (int i = fromIndex; i < toIndex; i++)
  468. a[i] = val;
  469. }
  470. public static void fill(float[] a, float val) {
  471. for (int i = 0, len = a.length; i < len; i++)
  472. a[i] = val;
  473. }
  474. public static void fill(float[] a, int fromIndex, int toIndex, float val) {
  475. rangeCheck(a.length, fromIndex, toIndex);
  476. for (int i = fromIndex; i < toIndex; i++)
  477. a[i] = val;
  478. }
  479. public static void fill(Object[] a, Object val) {
  480. for (int i = 0, len = a.length; i < len; i++)
  481. a[i] = val;
  482. }
  483. public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
  484. rangeCheck(a.length, fromIndex, toIndex);
  485. for (int i = fromIndex; i < toIndex; i++)
  486. a[i] = val;
  487. }
  488. @SuppressWarnings("unchecked")
  489. public static <T> T[] copyOf(T[] original, int newLength) {
  490. return (T[]) copyOf(original, newLength, original.getClass());
  491. }
  492. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
  493. @SuppressWarnings("unchecked")
  494. T[] copy = ((Object)newType == (Object)Object[].class)
  495. ? (T[]) new Object[newLength]
  496. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  497. System.arraycopy(original, 0, copy, 0,
  498. Math.min(original.length, newLength));
  499. return copy;
  500. }
  501. public static byte[] copyOf(byte[] original, int newLength) {
  502. byte[] copy = new byte[newLength];
  503. System.arraycopy(original, 0, copy, 0,
  504. Math.min(original.length, newLength));
  505. return copy;
  506. }
  507. public static short[] copyOf(short[] original, int newLength) {
  508. short[] copy = new short[newLength];
  509. System.arraycopy(original, 0, copy, 0,
  510. Math.min(original.length, newLength));
  511. return copy;
  512. }
  513. public static int[] copyOf(int[] original, int newLength) {
  514. int[] copy = new int[newLength];
  515. System.arraycopy(original, 0, copy, 0,
  516. Math.min(original.length, newLength));
  517. return copy;
  518. }
  519. public static long[] copyOf(long[] original, int newLength) {
  520. long[] copy = new long[newLength];
  521. System.arraycopy(original, 0, copy, 0,
  522. Math.min(original.length, newLength));
  523. return copy;
  524. }
  525. public static char[] copyOf(char[] original, int newLength) {
  526. char[] copy = new char[newLength];
  527. System.arraycopy(original, 0, copy, 0,
  528. Math.min(original.length, newLength));
  529. return copy;
  530. }
  531. public static float[] copyOf(float[] original, int newLength) {
  532. float[] copy = new float[newLength];
  533. System.arraycopy(original, 0, copy, 0,
  534. Math.min(original.length, newLength));
  535. return copy;
  536. }
  537. public static double[] copyOf(double[] original, int newLength) {
  538. double[] copy = new double[newLength];
  539. System.arraycopy(original, 0, copy, 0,
  540. Math.min(original.length, newLength));
  541. return copy;
  542. }
  543. public static boolean[] copyOf(boolean[] original, int newLength) {
  544. boolean[] copy = new boolean[newLength];
  545. System.arraycopy(original, 0, copy, 0,
  546. Math.min(original.length, newLength));
  547. return copy;
  548. }
  549. @SuppressWarnings("unchecked")
  550. public static <T> T[] copyOfRange(T[] original, int from, int to) {
  551. return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
  552. }
  553. public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
  554. int newLength = to - from;
  555. if (newLength < 0)
  556. throw new IllegalArgumentException(from + " > " + to);
  557. @SuppressWarnings("unchecked")
  558. T[] copy = ((Object)newType == (Object)Object[].class)
  559. ? (T[]) new Object[newLength]
  560. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  561. System.arraycopy(original, from, copy, 0,
  562. Math.min(original.length - from, newLength));
  563. return copy;
  564. }
  565. public static byte[] copyOfRange(byte[] original, int from, int to) {
  566. int newLength = to - from;
  567. if (newLength < 0)
  568. throw new IllegalArgumentException(from + " > " + to);
  569. byte[] copy = new byte[newLength];
  570. System.arraycopy(original, from, copy, 0,
  571. Math.min(original.length - from, newLength));
  572. return copy;
  573. }
  574. public static short[] copyOfRange(short[] original, int from, int to) {
  575. int newLength = to - from;
  576. if (newLength < 0)
  577. throw new IllegalArgumentException(from + " > " + to);
  578. short[] copy = new short[newLength];
  579. System.arraycopy(original, from, copy, 0,
  580. Math.min(original.length - from, newLength));
  581. return copy;
  582. }
  583. public static int[] copyOfRange(int[] original, int from, int to) {
  584. int newLength = to - from;
  585. if (newLength < 0)
  586. throw new IllegalArgumentException(from + " > " + to);
  587. int[] copy = new int[newLength];
  588. System.arraycopy(original, from, copy, 0,
  589. Math.min(original.length - from, newLength));
  590. return copy;
  591. }
  592. public static long[] copyOfRange(long[] original, int from, int to) {
  593. int newLength = to - from;
  594. if (newLength < 0)
  595. throw new IllegalArgumentException(from + " > " + to);
  596. long[] copy = new long[newLength];
  597. System.arraycopy(original, from, copy, 0,
  598. Math.min(original.length - from, newLength));
  599. return copy;
  600. }
  601. public static char[] copyOfRange(char[] original, int from, int to) {
  602. int newLength = to - from;
  603. if (newLength < 0)
  604. throw new IllegalArgumentException(from + " > " + to);
  605. char[] copy = new char[newLength];
  606. System.arraycopy(original, from, copy, 0,
  607. Math.min(original.length - from, newLength));
  608. return copy;
  609. }
  610. public static float[] copyOfRange(float[] original, int from, int to) {
  611. int newLength = to - from;
  612. if (newLength < 0)
  613. throw new IllegalArgumentException(from + " > " + to);
  614. float[] copy = new float[newLength];
  615. System.arraycopy(original, from, copy, 0,
  616. Math.min(original.length - from, newLength));
  617. return copy;
  618. }
  619. public static double[] copyOfRange(double[] original, int from, int to) {
  620. int newLength = to - from;
  621. if (newLength < 0)
  622. throw new IllegalArgumentException(from + " > " + to);
  623. double[] copy = new double[newLength];
  624. System.arraycopy(original, from, copy, 0,
  625. Math.min(original.length - from, newLength));
  626. return copy;
  627. }
  628. public static boolean[] copyOfRange(boolean[] original, int from, int to) {
  629. int newLength = to - from;
  630. if (newLength < 0)
  631. throw new IllegalArgumentException(from + " > " + to);
  632. boolean[] copy = new boolean[newLength];
  633. System.arraycopy(original, from, copy, 0,
  634. Math.min(original.length - from, newLength));
  635. return copy;
  636. }
  637. }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/502684
推荐阅读
相关标签
  

闽ICP备14008679号