数组与集合

本文介绍了Java中数组的深拷贝方法,包括System.arraycopy和Arrays.copyOf的具体实现,并通过示例说明了它们的区别。此外,还讲解了如何利用Arrays.asList和List.toArray方法实现数组与List之间的相互转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1、拷贝之Java.lang.System.arraycopy和java.util.Arrays.copyOf方法


Java数组的复制操作可以分为深复制和浅复制。一般对于引用类型的数组来说,需要在某些时候进行深复制。

 

首先来看一下两个方法实现的源代码,如下:


  1. public static <T> T[] copyOf(T[] original, int newLength) {  
  2.     return (T[]) copyOf(original, newLength, original.getClass());  
  3. }  
  4.   
  5. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {  
  6.     T[] copy = ((Object)newType == (Object)Object[].class)  
  7.         ? (T[]) new Object[newLength]   // 类型相同,则重新生成一个大小为newLength的数组实例  
  8.         : (T[]) Array.newInstance(newType.getComponentType(), newLength);  // 类型不同,重新生成一个大小为newLength的新类型数组实例  
  9.     System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //将原数组内容拷贝到新数组中,新数组取最小的数组长度  
  10.     return copy;  // 返回新数组的引用   
  11. }  
    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) {
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]   // 类型相同,则重新生成一个大小为newLength的数组实例
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);  // 类型不同,重新生成一个大小为newLength的新类型数组实例
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //将原数组内容拷贝到新数组中,新数组取最小的数组长度
        return copy;  // 返回新数组的引用 
    }

而系统方法是一个本地方法,如下:

  1. public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);  
 public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);
arraycopy 方法中,如果新数组比原数组的length小则会抛出java.lang.ArrayIndexOutOfBoundsException异常。

举个例子,如下:

  1. public class test11 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         String[] strArray = new String[] { "ab""xy""mn" };  
  5.         String[] copyArray = new String[2]; // 新数组大小比原数组小  
  6.         for (int i = 0; i < strArray.length; i++) {  
  7.             System.arraycopy(strArray, 0, copyArray, 0, strArray.length); // 调用系统方法进行深拷贝  
  8.         }  
  9.         printArray(copyArray);  
  10.         System.out.println(strArray == copyArray ? "浅拷贝" : "深拷贝");  
  11.   
  12.     }  
  13.     public static void printArray(String[] array) {  
  14.         for (int i = 0; i < array.length; i++) {  
  15.             System.out.print(array[i] + "  ");  
  16.         }  
  17.     }  
  18.   
  19. }  
public class test11 {

	public static void main(String[] args) {
		String[] strArray = new String[] { "ab", "xy", "mn" };
		String[] copyArray = new String[2]; // 新数组大小比原数组小
		for (int i = 0; i < strArray.length; i++) {
			System.arraycopy(strArray, 0, copyArray, 0, strArray.length); // 调用系统方法进行深拷贝
		}
		printArray(copyArray);
		System.out.println(strArray == copyArray ? "浅拷贝" : "深拷贝");

	}
	public static void printArray(String[] array) {
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + "  ");
		}
	}

}
会抛出系统,将copyArray的大小改为4时,输出的结果如下:

ab  xy  mn  null  深拷贝

使用copyOf()方法,如下:

  1. public class test11 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         String[] strArray = new String[] { "ab""xy""mn" };  
  5.         String[] copyArray=new String[4];    
  6.         for(int i=0;i<strArray.length;i++){    
  7.             copyArray=Arrays.copyOf(strArray, strArray.length);    
  8.         }    
  9.         printArray(copyArray);    
  10.         System.out.println(strArray==copyArray?"浅拷贝":"深拷贝");    
  11.           
  12.           
  13.     }  
  14.   
  15.     public static void printArray(String[] array) {  
  16.         for (int i = 0; i < array.length; i++) {  
  17.             System.out.print(array[i] + "  ");  
  18.         }  
  19.     }  
  20. }  
public class test11 {

	public static void main(String[] args) {
		String[] strArray = new String[] { "ab", "xy", "mn" };
		String[] copyArray=new String[4];  
		for(int i=0;i<strArray.length;i++){  
		    copyArray=Arrays.copyOf(strArray, strArray.length);  
		}  
		printArray(copyArray);  
		System.out.println(strArray==copyArray?"浅拷贝":"深拷贝");  
		
		
	}

	public static void printArray(String[] array) {
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + "  ");
		}
	}
}

如上输出:

ab  xy  mn  深拷贝

copyOf不会因为copyArray方法的大小而报错。因为copyOf 的返回值是在内部new 好的copy 数组,而该copy 数组new 的大小就等于newLength 。但是copyOf 方法最后也是调用System.arraycopy 的方法,不过由于之前已经对数组进行了处理,所以不会报出异常。 




2、数组与List相互转换


可以使用Arrays类中提供的asList()方法,如下:

  1. List<String>  list01 = Arrays.asList("Larry""Moe""Curly");  
  2.      String[] array ={"a","b","c"};  
  3.      List<String>  list2 =  Arrays.asList(array);  
 List<String>  list01 = Arrays.asList("Larry", "Moe", "Curly");
    	 String[] array ={"a","b","c"};
    	 List<String>  list2 =  Arrays.asList(array);
或者直接调用集合方法中的toArray()方法转换为数组,如下:

  1. String[] strs = (String[]) list01.toArray();  
 String[] strs = (String[]) list01.toArray();


转载自http://blog.youkuaiyun.com/mazhimazh/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值