您的位置:首页 > 其它

Arrays

2015-10-22 20:16 239 查看
System.arraycopy---测试如下:

public class ArrayTest {
public static void main(String[] args) {
char[] src = {'a','b','c'};
char[] dest = new char[5];
System.arraycopy(src, 0, dest, 1, 2);
System.out.println(Arrays.toString(dest));
}

}


输出结果:[ ,a,b, , ]

Arrays.copyOfRange--源码如下:

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 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;
}


Arrays.binarySearch

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];

int cmp;
if (midVal < key) {
cmp = -1;   // Neither val is NaN, thisVal is smaller
} else if (midVal > key) {
cmp = 1;    // Neither val is NaN, thisVal is larger
} else {
int midBits = Float.floatToIntBits(midVal);
int keyBits = Float.floatToIntBits(key);
cmp = (midBits == keyBits ?  0 : // Values are equal
(midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1));                     // (0.0, -0.0) or (NaN, !NaN)
}

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


>>> 表示二进制右移(>>>1相当于/2)

计算机表示浮点数(float或者double类型)都有一个精度限制,对于超出了精度限制的浮点数,计算机会把他们超出精度之外的小数部分截掉,这样不想等的两个浮点数在计算机中用==进行比较的时候就有可能相等了。

因此,对于浮点数的比较可以直接使用>或者<,但是不能直接使用==。

对于float型的比较,先用Float.floatToIntBits转换成int类型的值,然后再用==操作符进行比较。

对于double型的比较,先用Double.doubleToLongBits转成long类型的值,然后再使用==操作符进行比较。

对于int的可以直接进行比较的,源码如下:

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.
}


Arrays.equals 比较各种类型的数组对象是否相等(== 是比较地址空间,equals比较的是内容),此处只罗列值得关注的几个

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(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;
}


Arrays.fill

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;
}


Arrays.copyOf

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;
}


Arrays.copyOfRange

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: