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

java中有三种移位运算符

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

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

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

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

深入学习java源码之Arrays.binarySearch()与Arrays.copyOf()_搜索算法

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

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

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

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

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

// 20
System.out.println(10 << 1);
// -20
System.out.println(-10 << 1);
// 5
System.out.println(10 >> 1);
// -5
System.out.println(-10 >> 1);
// 5
System.out.println(10 >>> 1);
// 2147483643
System.out.println(-10 >>> 1);

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

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

System.arraycopy()

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

System.out.println(Array.toString(copied));
运行结果如下:
[0,1,2,3,4,5,0,0,0,0];

arrays.copyof()

int[] arr = {1,2,3,4,5};  
int copied=arrays.copyof(arr,10);
System.out.println(Arrays.toString(copied));
copied=arrays.copyof(arr,3);
System.out.println(Arrays.toString(copied));

运行结果如下:
[1,2,3,4,5,0,0,0,0,0]
[1,2,3]

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

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

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

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

 

Modifier and Type

Method and Description

​static int​

​binarySearch(byte[] a, byte key)​

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

​static int​

​binarySearch(byte[] a, int fromIndex, int toIndex, byte key)​

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

​static int​

​binarySearch(char[] a, char key)​

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

​static int​

​binarySearch(char[] a, int fromIndex, int toIndex, char key)​

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

​static int​

​binarySearch(double[] a, double key)​

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

​static int​

​binarySearch(double[] a, int fromIndex, int toIndex, double key)​

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

​static int​

​binarySearch(float[] a, float key)​

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

​static int​

​binarySearch(float[] a, int fromIndex, int toIndex, float key)​

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

​static int​

​binarySearch(int[] a, int key)​

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

​static int​

​binarySearch(int[] a, int fromIndex, int toIndex, int key)​

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

​static int​

​binarySearch(long[] a, int fromIndex, int toIndex, long key)​

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

​static int​

​binarySearch(long[] a, long key)​

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

​static int​

​binarySearch(Object[] a, int fromIndex, int toIndex, Object​

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

​static int​

​binarySearch(Object[] a, Object​

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

​static int​

​binarySearch(short[] a, int fromIndex, int toIndex, short key)​

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

​static int​

​binarySearch(short[] a, short key)​

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

​static <T> int​

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

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

​static <T> int​

​binarySearch(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 boolean​

​deepEquals(Object[] a1, Object[] a2)​

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

​static int​

​deepHashCode(Object[] a)​

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

​static String​

​deepToString(Object[] a)​

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

​static boolean​

​equals(boolean[] a, boolean[] a2)​

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

​static boolean​

​equals(byte[] a, byte[] a2)​

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

​static boolean​

​equals(char[] a, char[] a2)​

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

​static boolean​

​equals(double[] a, double[] a2)​

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

​static boolean​

​equals(float[] a, float[] a2)​

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

​static boolean​

​equals(int[] a, int[] a2)​

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

​static boolean​

​equals(long[] a, long[] a2)​

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

​static boolean​

​equals(Object[] a, Object[] a2)​

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

​static boolean​

​equals(short[] a, short[] a2)​

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

​static void​

​fill(boolean[] a, boolean val)​

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

​static void​

​fill(boolean[] a, int fromIndex, int toIndex, boolean val)​

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

​static void​

​fill(byte[] a, byte val)​

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

​static void​

​fill(byte[] a, int fromIndex, int toIndex, byte val)​

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

​static void​

​fill(char[] a, char val)​

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

​static void​

​fill(char[] a, int fromIndex, int toIndex, char val)​

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

​static void​

​fill(double[] a, double val)​

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

​static void​

​fill(double[] a, int fromIndex, int toIndex, double val)​

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

​static void​

​fill(float[] a, float val)​

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

​static void​

​fill(float[] a, int fromIndex, int toIndex, float val)​

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

​static void​

​fill(int[] a, int val)​

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

​static void​

​fill(int[] a, int fromIndex, int toIndex, int val)​

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

​static void​

​fill(long[] a, int fromIndex, int toIndex, long val)​

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

​static void​

​fill(long[] a, long val)​

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

​static void​

​fill(Object[] a, int fromIndex, int toIndex, Object​

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

​static void​

​fill(Object[] a, Object​

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

​static void​

​fill(short[] a, int fromIndex, int toIndex, short val)​

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

​static void​

​fill(short[] a, short val)​

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

java源码

package java.util;

import java.lang.reflect.Array;
import java.util.concurrent.ForkJoinPool;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.DoubleBinaryOperator;
import java.util.function.IntBinaryOperator;
import java.util.function.IntFunction;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntToLongFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.LongBinaryOperator;
import java.util.function.UnaryOperator;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Arrays {

private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;

// Suppresses default constructor, ensuring non-instantiability.
private Arrays() {}

static final class NaturalOrder implements Comparator<Object> {
@SuppressWarnings("unchecked")
public int compare(Object first, Object second) {
return ((Comparable<Object>)first).compareTo(second);
}
static final NaturalOrder INSTANCE = new NaturalOrder();
}

private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
if (fromIndex > toIndex) {
throw new IllegalArgumentException(
"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
if (fromIndex < 0) {
throw new ArrayIndexOutOfBoundsException(fromIndex);
}
if (toIndex > arrayLength) {
throw new ArrayIndexOutOfBoundsException(toIndex);
}
}

public static int binarySearch(long[] a, long key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(long[] a, int fromIndex, int toIndex,
long key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(long[] a, int fromIndex, int toIndex,
long key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
long midVal = a[mid];

if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static int binarySearch(int[] a, int key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(int[] a, int fromIndex, int toIndex,
int key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];

if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static int binarySearch(short[] a, short key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(short[] a, int fromIndex, int toIndex,
short key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(short[] a, int fromIndex, int toIndex,
short key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
short midVal = a[mid];

if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static int binarySearch(char[] a, char key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(char[] a, int fromIndex, int toIndex,
char key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(char[] a, int fromIndex, int toIndex,
char key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
char midVal = a[mid];

if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static int binarySearch(byte[] a, byte key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(byte[] a, int fromIndex, int toIndex,
byte key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
byte key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
byte midVal = a[mid];

if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static int binarySearch(double[] a, double key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(double[] a, int fromIndex, int toIndex,
double key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(double[] a, int fromIndex, int toIndex,
double key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
double midVal = a[mid];

if (midVal < key)
low = mid + 1; // Neither val is NaN, thisVal is smaller
else if (midVal > key)
high = mid - 1; // Neither val is NaN, thisVal is larger
else {
long midBits = Double.doubleToLongBits(midVal);
long keyBits = Double.doubleToLongBits(key);
if (midBits == keyBits) // Values are equal
return mid; // Key found
else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
low = mid + 1;
else // (0.0, -0.0) or (NaN, !NaN)
high = mid - 1;
}
}
return -(low + 1); // key not found.
}

public static int binarySearch(float[] a, float key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(float[] a, int fromIndex, int toIndex,
float key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(float[] a, int fromIndex, int toIndex,
float key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
float midVal = a[mid];

if (midVal < key)
low = mid + 1; // Neither val is NaN, thisVal is smaller
else if (midVal > key)
high = mid - 1; // Neither val is NaN, thisVal is larger
else {
int midBits = Float.floatToIntBits(midVal);
int keyBits = Float.floatToIntBits(key);
if (midBits == keyBits) // Values are equal
return mid; // Key found
else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
low = mid + 1;
else // (0.0, -0.0) or (NaN, !NaN)
high = mid - 1;
}
}
return -(low + 1); // key not found.
}

public static int binarySearch(Object[] a, Object key) {
return binarySearch0(a, 0, a.length, key);
}

public static int binarySearch(Object[] a, int fromIndex, int toIndex,
Object key) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key);
}

// Like public version, but without range checks.
private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
Object key) {
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
@SuppressWarnings("rawtypes")
Comparable midVal = (Comparable)a[mid];
@SuppressWarnings("unchecked")
int cmp = midVal.compareTo(key);

if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
return binarySearch0(a, 0, a.length, key, c);
}

public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
T key, Comparator<? super T> c) {
rangeCheck(a.length, fromIndex, toIndex);
return binarySearch0(a, fromIndex, toIndex, key, c);
}

// Like public version, but without range checks.
private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
T key, Comparator<? super T> c) {
if (c == null) {
return binarySearch0(a, fromIndex, toIndex, key);
}
int low = fromIndex;
int high = toIndex - 1;

while (low <= high) {
int mid = (low + high) >>> 1;
T midVal = a[mid];
int cmp = c.compare(midVal, key);
if (cmp < 0)
low = mid + 1;
else if (cmp > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

public static boolean equals(long[] a, long[] 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++)
if (a[i] != a2[i])
return false;

return true;
}

public static boolean equals(int[] a, int[] 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++)
if (a[i] != a2[i])
return false;

return true;
}

public static boolean equals(short[] a, short 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++)
if (a[i] != a2[i])
return false;

return true;
}

public static boolean equals(char[] a, char[] 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++)
if (a[i] != a2[i])
return false;

return true;
}

public static boolean equals(byte[] a, byte[] 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++)
if (a[i] != a2[i])
return false;

return true;
}
public static boolean equals(boolean[] a, boolean[] 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++)
if (a[i] != a2[i])
return false;

return true;
}

public static boolean equals(double[] a, double[] 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++)
if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
return false;

return true;
}

public static boolean equals(float[] a, float[] 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++)
if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
return false;

return true;
}

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 void fill(long[] a, long val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(long[] a, int fromIndex, int toIndex, long val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

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(int[] a, int fromIndex, int toIndex, int val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(short[] a, short val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(short[] a, int fromIndex, int toIndex, short val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
public static void fill(char[] a, int fromIndex, int toIndex, char val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(byte[] a, byte val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(boolean[] a, boolean val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(boolean[] a, int fromIndex, int toIndex,
boolean val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(double[] a, double val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(double[] a, int fromIndex, int toIndex,double val){
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(float[] a, float val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(float[] a, int fromIndex, int toIndex, float val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

public static void fill(Object[] a, Object val) {
for (int i = 0, len = a.length; i < len; i++)
a[i] = val;
}

public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}

@SuppressWarnings("unchecked")
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

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 short[] copyOf(short[] original, int newLength) {
short[] copy = new short[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static long[] copyOf(long[] original, int newLength) {
long[] copy = new long[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static float[] copyOf(float[] original, int newLength) {
float[] copy = new float[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static double[] copyOf(double[] original, int newLength) {
double[] copy = new double[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

public static boolean[] copyOf(boolean[] original, int newLength) {
boolean[] copy = new boolean[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}

@SuppressWarnings("unchecked")
public static <T> T[] copyOfRange(T[] original, int from, int to) {
return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
}

public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static short[] copyOfRange(short[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
short[] copy = new short[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static int[] copyOfRange(int[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
int[] copy = new int[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static long[] copyOfRange(long[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
long[] copy = new long[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static float[] copyOfRange(float[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
float[] copy = new float[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static double[] copyOfRange(double[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
double[] copy = new double[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

public static boolean[] copyOfRange(boolean[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
boolean[] copy = new boolean[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

}