Java Reference
In-Depth Information
Display 15.16
Demonstration of Deep Copy
clone
1
public class
DeepDemo
2 {
3
public static void
main(String[] args)
4 {
5 LinkedList<StockItem> originalList =
6
new
LinkedList<StockItem>( );
7 originalList.addToStart(
new
StockItem("red dress", 1));
8 originalList.addToStart(
new
StockItem("black shoe", 2));
9 LinkedList<StockItem> copyList = originalList.clone( );
10 if (originalList.equals(copyList))
11 System.out.println("OK, Lists are equal.");
12 System.out.println("Now we change copyList.");
13 StockItem dataEntry =
14 copyList.findData(
new
StockItem("red dress", 1));
15 dataEntry.setName("orange pants");
16 System.out.println("originalList:");
17 originalList.outputList( );
18 System.out.println("copyList:");
19 copyList.outputList( );
20
21 System.out.println("Only one list is changed.");
22 }
23 }
Sample Dialogue
OK, Lists are equal.
Now we change copyList.
originalList:
black shoe 2
red dress 1
copyList:
black shoe 2
orange pants 1
Only one list is changed.