方法clone()在Java中做什么?
java.util.ArrayList类的clone()方法返回此ArrayList实例的浅表副本(即,不复制元素本身)。
示例
import java.util.ArrayList;public class ArrayListDemo {
public static void main(String args[]) {
ArrayList<StringBuilder> arrlist1 = new ArrayList<StringBuilder>();
arrlist1.add(new StringBuilder("Learning-"));
ArrayList arrlist2 = (ArrayList) arrlist1.clone();
StringBuilder strbuilder = arrlist1.get(0);
strbuilder.append("list1, list2-both pointing to the same StringBuilder");
System.out.println("The 1st list prints: ");
for (int i = 0; i < arrlist1.size(); i++) {
System.out.print(arrlist1.get(i) + " ");
}
System.out.println("The 2nd list prints the same i.e:");
for (int i = 0; i < arrlist2.size(); i++) {
System.out.print(arrlist2.get(i));
}
}
}
输出结果
The 1st list prints:Learning-list1, list2 - both pointing to the same StringBuilder
The 2nd list prints the same i.e:
Learning-list1, list2 - both pointing to the same StringBuilder
以上是 方法clone()在Java中做什么? 的全部内容, 来源链接: utcz.com/z/361096.html