Arrays

System.arraycopy---测试如下:

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

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

Arrays.copyOfRange--源码如下:

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

 Arrays.binarySearch

 1  public static int binarySearch(float[] a, int fromIndex, int toIndex,
 2                    float key) {
 3     rangeCheck(a.length, fromIndex, toIndex);
 4     return binarySearch0(a, fromIndex, toIndex, key);
 5     }
 6 
 7     // Like public version, but without range checks.
 8     private static int binarySearch0(float[] a, int fromIndex, int toIndex,
 9                      float key) {
10     int low = fromIndex;
11     int high = toIndex - 1;
12 
13     while (low <= high) {
14         int mid = (low + high) >>> 1;
15         float midVal = a[mid];
16 
17             int cmp;
18             if (midVal < key) {
19                 cmp = -1;   // Neither val is NaN, thisVal is smaller
20             } else if (midVal > key) {
21                 cmp = 1;    // Neither val is NaN, thisVal is larger
22             } else {
23                 int midBits = Float.floatToIntBits(midVal);
24                 int keyBits = Float.floatToIntBits(key);
25                 cmp = (midBits == keyBits ?  0 : // Values are equal
26                        (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
27                         1));                     // (0.0, -0.0) or (NaN, !NaN)
28             }
29 
30         if (cmp < 0)
31         low = mid + 1;
32         else if (cmp > 0)
33         high = mid - 1;
34         else
35         return mid; // key found
36     }
37     return -(low + 1);  // key not found.
38     }

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

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

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

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

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

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

 1   private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
 2                      byte key) {
 3     int low = fromIndex;
 4     int high = toIndex - 1;
 5 
 6     while (low <= high) {
 7         int mid = (low + high) >>> 1;
 8         byte midVal = a[mid];
 9 
10         if (midVal < key)
11         low = mid + 1;
12         else if (midVal > key)
13         high = mid - 1;
14         else
15         return mid; // key found
16     }
17     return -(low + 1);  // key not found.
18     }

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

 1   public static boolean equals(double[] a, double[] a2) {
 2         if (a==a2)
 3             return true;
 4         if (a==null || a2==null)
 5             return false;
 6 
 7         int length = a.length;
 8         if (a2.length != length)
 9             return false;
10 
11         for (int i=0; i<length; i++)
12         if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
13                 return false;
14 
15         return true;
16     }
 1  public static boolean equals(Object[] a, Object[] a2) {
 2         if (a==a2)
 3             return true;
 4         if (a==null || a2==null)
 5             return false;
 6 
 7         int length = a.length;
 8         if (a2.length != length)
 9             return false;
10 
11         for (int i=0; i<length; i++) {
12             Object o1 = a[i];
13             Object o2 = a2[i];
14             if (!(o1==null ? o2==null : o1.equals(o2)))
15                 return false;
16         }
17 
18         return true;
19     }

 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

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

 Arrays.copyOfRange

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

 

posted @ 2015-10-22 20:16  笨狗毒毒毒  阅读(217)  评论(0编辑  收藏  举报