使用clone()方法复制数组

发表于: java/j2ee | 作者: darkmi | 日期: 2011/03/11 08:00
字号:

在java中数组复制有三种方法:

1、使用FOR循环,这个比较简单略过不表;

2、使用System.arraycopy(s,start1,t,start2,length)方法;

已经总结过,见如下链接:

System.arraycopy()使用小记

3。使用clone方法。

示例如下:

package com.darkmi.basic;

public class ArrayCopy {

	public static void main(String[] args) {
		String[] strs1 = { "aaa", "bbb", "ccc" };

		String[] strs2 = (String[]) strs1.clone();

		strs1[0] = "ddd";

		for (int i = 0; i < strs1.length; i++) {
			System.out.println("strs1[" + i + "] = " + strs1[i]);

		}

		for (int i = 0; i < strs2.length; i++) {
			System.out.println("strs2[" + i + "] = " + strs2[i]);
		}

	}

}

关于clone()方法的详细介绍参考如下链接:

http://www.javaeye.com/topic/182772

http://www.javaeye.com/topic/483469

切换主题切换主题
返回顶部