「Java」java.util.ArrayList.clone()の使い方

Javaコード
package excers;

import java.util.ArrayList;

public class ArrayListCloneDemo {

public static void main(String[] args) {
// create an empty array list
ArrayList<StringBuilder> arrLstOne = new ArrayList<StringBuilder>();

// use add for new value
arrLstOne.add(new StringBuilder(“検証情報"));

// using clone to affect the objects pointed to by the references.
@SuppressWarnings(“rawtypes")
ArrayList arrLstTwo = (ArrayList) arrLstOne.clone();

// appending the string
StringBuilder strbuilder = arrLstOne.get(0);
strbuilder.append(“list1, list2-both pointing to the same StringBuilder");

System.out.println(“List1の要素: “);

// both lists will print the same value, printing list1
for (int i = 0; i < arrLstOne.size(); i++) {
System.out.print(arrLstOne.get(i) + " “);
}

System.out.println(“\nListのコピー後List2の要素:");

// both lists will print the same value, printing list2
for (int i = 0; i < arrLstTwo.size(); i++) {
System.out.print(arrLstTwo.get(i));
}
}

}

実行結果
List1の要素:
検証情報list1, list2-both pointing to the same StringBuilder
Listのコピー後List2の要素:
検証情報list1, list2-both pointing to the same StringBuilder

Java

Posted by arkgame