描述
這個java.util.ArrayList.clone()返回這個 ArrayList 實例的淺拷貝(即元素本身不被複製)。
聲明
以下是聲明java.util.ArrayList.clone()方法
public Object clone()
參數
NA
返回值
此方法返回此 ArrayList 實例的副本。
異常
NA
示例
下麵的例子展示了 java.util.Arraylist.clone() 方法的用法。
package com.tutorialspoint;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String args[]) {
// create an empty array list
ArrayList<StringBuilder> arrlist1 = new ArrayList<StringBuilder>();
// use add for new value
arrlist1.add(new StringBuilder("Learning-"));
// using clone to affect the objects pointed to by the references.
ArrayList arrlist2 = (ArrayList) arrlist1.clone();
// appending the string
StringBuilder strbuilder = arrlist1.get(0);
strbuilder.append("list1, list2-both pointing to the same StringBuilder");
System.out.println("The 1st list prints:");
// both lists will print the same value, printing list1
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:");
// both lists will print the same value, printing list2
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
相關用法
- Java Java.util.ArrayList.clear()用法及代碼示例
- Java Java.util.ArrayList.contains()用法及代碼示例
- Java Java.util.ArrayList.remove()用法及代碼示例
- Java Java.util.ArrayList.size()用法及代碼示例
- Java Java.util.ArrayList.addAll()用法及代碼示例
- Java Java.util.ArrayList.removeRange()用法及代碼示例
- Java Java.util.ArrayList.addall()用法及代碼示例
- Java Java.util.ArrayList.lastIndexOf()用法及代碼示例
- Java Java.util.ArrayList.trimToSize()用法及代碼示例
- Java Java.util.ArrayList.indexOf()用法及代碼示例
- Java Java.util.ArrayList.toArray()用法及代碼示例
- Java Java.util.ArrayList.add()用法及代碼示例
- Java Java.util.ArrayList.isEmpty()用法及代碼示例
- Java Java.util.ArrayList.get()用法及代碼示例
- Java Java.util.ArrayList.ensureCapacity()用法及代碼示例
- Java Java.util.ArrayList.set()用法及代碼示例
- Java Java.util.ArrayDeque.pop()用法及代碼示例
- Java Java.util.ArrayDeque.toArray()用法及代碼示例
- Java Java.util.Arrays.deepEquals()用法及代碼示例
- Java Java.util.Arraylist.indexOf()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.ArrayList.clone() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。