Java数组复制小结_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java数组复制小结

Java数组复制小结

 2013/11/24 18:26:04  lijingshou  程序员俱乐部  我要评论(0)
  • 摘要:在Java中可以使用Arrays.copyOf()方法和System.arraycopy进行数组复制与扩容,以下是例子:importjava.lang.reflect.Array;importjava.util.Arrays;publicclassArrayCopyTest{publicstaticvoidmain(String[]args){//将源数组复制并且扩容String[]arr={"james","dajun","tom","Lily"};arr=Arrays.copyOf(arr
  • 标签:数组 Java 复制

在Java中可以使用Arrays.copyOf()方法和System.arraycopy进行数组复制与扩容,以下是例子

class="java">import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayCopyTest {

	public static void main(String[] args) {
		// 将源数组复制并且扩容
		String[] arr ={"james", "dajun", "tom", "Lily"};
		arr =Arrays.copyOf(arr, 10);
		System.out.println(Arrays.toString(arr));
		System.out.println("The length of new array is:" + arr.length);
		
		// 将源数组复制到目标数组,并且制定复制的长度
		String[] dest = new String[5];
		System.arraycopy(arr, 0, dest, 0, dest.length);
		System.out.println(Arrays.toString(dest));
		
		// 使用goodArrayCopy复制,并且制定新容量, dest的长度从5扩大到10
		dest = (String[])goodArrayGrow(dest, 10);
		System.out.println(Arrays.toString(dest));
	}
	    // 此处整个数组被当成Object传入,newLength必须大于原先数组长度
	public static Object goodArrayGrow(Object srcArr, int newLength){
		Class arrClass = srcArr.getClass();
		if (!arrClass.isArray())
			return null;
		Class arrType = arrClass.getComponentType();
		int length = Array.getLength(srcArr);
		
		Object newArray = Array.newInstance(arrType, newLength);
		System.arraycopy(srcArr, 0, newArray, 0, length);
		return newArray;		
	}

}

?

程序运行结果如下:

[james, dajun, tom, Lily, null, null, null, null, null, null]
The length of new array is:10
[james, dajun, tom, Lily, null]
[james, dajun, tom, Lily, null, null, null, null, null, null]

??

再看Arrays.copyOf()的源代码,本质上还是调用了System.arraycopy()方法:

/**
     * Copies the specified array, truncating or padding with nulls (if necessary)
     * so the copy has the specified length.  For all indices that are
     * valid in both the original array and the copy, the two arrays will
     * contain identical values.  For any indices that are valid in the
     * copy but not the original, the copy will contain <tt>null</tt>.
     * Such indices will exist if and only if the specified length
     * is greater than that of the original array.
     * The resulting array is of the class <tt>newType</tt>.
     *
     * @param original the array to be copied
     * @param newLength the length of the copy to be returned
     * @param newType the class of the copy to be returned
     * @return a copy of the original array, truncated or padded with nulls
     *     to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException if <tt>original</tt> is null
     * @throws ArrayStoreException if an element copied from
     *     <tt>original</tt> is not of a runtime type that can be stored in
     *     an array of class <tt>newType</tt>
     * @since 1.6
     */
    public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        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;
    }

?而System.arraycopy是个native方法,不是用Java语言实现的:

 public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

?

此外,在<Java核心技术>第一卷里作者提供了一个通用的goodArrayGrow()方法,此方法的使用与Arrays.copyOf()类似,在例子略作了修改.

?

?

?

上一篇: Service详解 下一篇: 没有下一篇了!
发表评论
用户名: 匿名