String[] x={"a","c"};
String[] y={"m","n"};
LinkedList
list=new LinkedList<>();
list.add(x);
list.add(y);
LinkedList
l=(LinkedList
) list.clone(); l.get(1)[1]="ttttt"; System.out.println(list.get(1)[1]);
这时候打印出的值为ttt,也就是修改克隆后的引用值,原值也会被修改,这时候就要用深度克隆了。如果细心且阅读过源代码的人就会发现,有些方法克隆链表元素的方法其实和clone方法是一样的,如下:
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node
x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
publicT[] toArray(T[] a) { if (a.length < size) a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); int i = 0; Object[] result = a; for (Node x = first; x != null; x = x.next) result[i++] = x.item; if (a.length > size) a[size] = null; return a; }