Arrays实用功能(上)

前言    


在Java.util类库中可以找到Arrays类,它有一套用于数组的static实用方法,其中有六个基本方法;
  • equals():用于比较两个数组是否相等(deepEquals()用于多维数组);
  • fill():用于指定数组中的元素;
  • sort():用于对数组排序;
  • binarySearch():用于在已经排序的数组中查找元素(二分查找);
  • toString():产生数组的String表示;
  • hashCode():产生数组的散列码。

所有这些方法对各种基本类型和Object类而重载过。此外,Aarrays.asList()接受任意的序列或数组作为其参数,并将其转变为List容器。

复制数组


Java标准类库提供有static方法System.arraycopy(),用它复制数组比用for循环复制要快很多。System.arraycopy()针对所有类型做了重载。下面的例子就是用来处理int数组的。
     
     
import java.util.Arrays;
 
public class CopyingArrays {
 
public static void main(String[] args) {
int[] i = new int[7];
int[] j = new int[10];
Arrays.fill(i, 47); // 对数组赋值
Arrays.fill(j, 99);
System.out.println("i = " + Arrays.toString(i)); // 打印数组
System.out.println("j = " + Arrays.toString(j));
System.arraycopy(i, 0, j, 0, i.length); //复制数组
System.out.println("j = " + Arrays.toString(j));
int[] k = new int[5];
Arrays.fill(k, 103);
System.arraycopy(i, 0, k, 0, k.length);
System.out.println("k = " + Arrays.toString(k));
Arrays.fill(k, 103);
System.arraycopy(k, 0, i, 0, k.length);
System.out.println("i = " + Arrays.toString(i));
// Object
Integer[] u = new Integer[10];
Integer[] v = new Integer[5];
Arrays.fill(u, new Integer(47));
Arrays.fill(v, new Integer(99));
System.out.println("u = " + Arrays.toString(u));
System.out.println("v = " + Arrays.toString(v));
System.arraycopy(v, 0, u, u.length / 2, v.length);
System.out.println("u = " + Arrays.toString(u));
}
 
}
打印结果如下:
      
      
i = [47, 47, 47, 47, 47, 47, 47]
j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]
k = [47, 47, 47, 47, 47]
i = [103, 103, 103, 103, 103, 47, 47]
u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]
v = [99, 99, 99, 99, 99]
u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]
其中System.arraycopy()参数说明如下:
     
     
* @param src the source array. // 源数组
* @param srcPos starting position in the source array. // 从源数组中的什么位置开始复制的偏移量
* @param dest the destination array.   // 目标数组
* @param destPos starting position in the destination data. // 从目标数组中的什么位置开始复制的偏移量
* @param length the number of array elements to be copied. // 需要复制的元素个数
* @exception IndexOutOfBoundsException if copying would cause // 如果复制会导致对数组范围以外的数据的访问
* access of data outside array bounds.
* @exception ArrayStoreException if an element in the <code>src</code> //如果因为类型不匹配而使得无法将 src 数组中的元素存储                                                                                 到 dest 数组中。
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or // 如果 src 或 dest 为 null
* <code>dest</code> is <code>null</code>.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
 这个例子说明基本类型数组与对象数组都可以复制。然而,如果复制对象数组,那么只是复制了对象的引用----而不是对象本身的拷贝。这被称为”浅复制“。
System.arraycopy()不会执行自动包装和自动拆包,两个数组必须具有相同的确切类型。

数组的比较


Arrays类提供了重载后的equals()方法,用来比较整个数组。同样,此方法针对所有基本类型与Object都做了重载。数组相等的条件是元素个数必须相等,并且对应位置的元素也相等。这可以通过对每一个元素使用equals0作比较来判断。(对于基本类型,需要使用基本类型的包装器类的equals()方法,例如,对于int类型使用Integer.equals()作比较)见下例:
     
     
import java.util.Arrays;
 
public class ComparingArrays {
 
public static void main(String[] args) {
int[] a1 = new int[10];
int[] a2 = new int[10];
Arrays.fill(a1, 47);
Arrays.fill(a2, 47);
System.out.println(Arrays.equals(a1, a2)); // 比较基本类型数组
a2[3] = 11;
System.out.println(Arrays.equals(a1, a2));
String[] s1 = new String[4];
Arrays.fill(s1, "hi");
String[] s2 = {new String("hi"),new String("hi"),new String("hi"),new String("hi")};
System.out.println(Arrays.equals(s1, s2)); // 比较对象数组
}
 
}
打印结果如下:
     
     
true
false
true
最初,a1和a2完全相等,所以输出为true;然后改变其中一个元素,使得结果为false。在最后一个例子中,s1的所有元素都指向同一个对象,而数组s2包含五个相互独立的对象。然而,数组相等是基于内容的(Object.equals()比较),所以结果为true。
Arrays.equals()源码如下:
     
     
/**
* Returns <tt>true</tt> if the two specified arrays of ints are
* <i>equal</i> to one another. Two arrays are considered equal if both
* arrays contain the same number of elements, and all corresponding pairs
* of elements in the two arrays are equal. In other words, two arrays
* are equal if they contain the same elements in the same order. Also,
* two array references are considered equal if both are <tt>null</tt>.<p>
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return <tt>true</tt> if the two arrays are equal
*/
public static boolean equals(int[] a, int[] a2) {
if (a==a2) // 对象地址相等,返回true
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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值