java的LinkedList问题

GUILTYxc 2018-04-01 03:33:53
LinkedList中clone函数是复制出一个新的LinkedList对象,并且如果对复制出来的对象操作不会影响到原对象吗?
初学java还望指教
...全文
599 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
stacksoverflow 2018-07-25
  • 打赏
  • 举报
回复
会,linklist做的是浅拷贝,参考我下面的回答(9楼)
https://bbs.csdn.net/topics/392396551
kampoo 2018-04-02
  • 打赏
  • 举报
回复
通常在 浅复制 和 深复制 的使用上容易产生错误,尤其是列表/Set/Map的使用上。 浅复制 - 只复制列表/Set/Map的指针,不复制其中的元素。这种情况下,如果更改其中的元素,会发现克隆对象的内容也改变了~ 其实是它的指向变了。 深复制 - 复制列表/Set/Map及其中的元素。这种情况下,更改原来列表/Set/Map中的元素,不会影响克隆对象的内容 ~ 因为克隆对象指向了新的元素。
幽饮烛 2018-04-02
  • 打赏
  • 举报
回复
clone 是浅克隆,会新建一个 list 对象,对新的list对象操作不会影响到旧的list对象,但对新list里面的对象进行操作,会影响旧的list里面的对象,因为新旧list里面的对象是相同的。
自由自在_Yu 2018-04-02
  • 打赏
  • 举报
回复
public static void main(String[] args) {
		LinkedList<Integer> list = new LinkedList<Integer>();
		list.add(1);
		LinkedList<Integer> copy = new LinkedList<Integer>();
		copy = (LinkedList<Integer>) list.clone();
		copy.add(2);
		System.out.println("list:"+list); //[1]
		System.out.println("copy:"+copy); //[1,2]
		System.out.println("---------------------");
		LinkedList<Integer> list2 = new LinkedList<Integer>();
		list2 = list;
		list2.add(3);
		System.out.println("list:"+list);//[1,3]
		System.out.println("list2:"+list2);//[1,3]
	}
CrazyCoder1992 2018-04-02
  • 打赏
  • 举报
回复
楼主的理解有误区。 1. list中保存的是指向对象的引用,也就是元素对象所在的内存地址,并不是元素对象的值; 2. 执行clone方法时,实际上是创建一个新的list对象,然后添加原list中所有的元素对象引用到新的list中,并不会对元素对象进行克隆; 3. 如何理解list的clone方法:对新的list进行排序,添加/移除等不影响元素对象的值的操作,不会对原list产生影响。
GUILTYxc 2018-04-01
  • 打赏
  • 举报
回复
上边太乱了看这个吧
import java.util.LinkedList;
import java.util.Scanner;
public class Main {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int T = sc.nextInt();   //T组测试数据
		for(int i = 0; i < T; i++){
		LinkedList<int[]> list1 = new LinkedList<>();
		LinkedList<int[]> list2 = new LinkedList<>();
		int f = sc.nextInt();   //第一个多项式的长度
		int s = sc.nextInt();   //第二个的
		
		//给多项式赋值,分别是系数和指数
		int ii = 0;
		while(ii < f){
			int[] po = new int[2];
			po[0] = sc.nextInt();
			po[1] = sc.nextInt();
			list1.add(po);
			ii++;
		}
		for(int lp = 0; lp < s ;lp++){
			int[] pi = new int[2];
			pi[0] = sc.nextInt();
			pi[1] = sc.nextInt();
			list2.add(pi);
		}
		System.out.println(list1.size());   //输出一下多项式的长度
		System.out.println(list2.size());
		
		//把list2复制出来一份,以后除了输出都对temp操作,不改变list2的值
		LinkedList<int[]> temp = new LinkedList<>();
		temp = (LinkedList<int[]>) list2.clone();
		
		
		int q = sc.nextInt();   //一共几次操作,其中1代表输出,2多项式相加结果存到list1中,3相减
		for(int iii = 0; iii < q; iii++){
			int qq = sc.nextInt();
			if(qq == 1) mylist.outPut(list1, list2);
			else if(qq == 2) mylist.add(list1, temp);
			
			else if(qq == 3) mylist.min(list1, temp);
		}
	}
	}

}

class mylist{
	
	//输出函数的实现
	public static void outPut(LinkedList<int[]> list11, LinkedList<int[]> list22){
		for(int i = 0;i < list11.size(); i++){
			if(list11.get(i)[0] > 0&&i > 0&&list11.get(i)[0] != 0&&list11.get(i)[0] != 100 )    System.out.print("+");
			if(list11.get(i)[0] != 0&&list11.get(i)[0] != 100  )   System.out.print(list11.get(i)[0]+"X^"+list11.get(i)[1]);
			
		}
		System.out.println();
		for(int i = 0; i < list22.size(); i++){
			System.out.print(list22.get(i)[0]+"X^"+list22.get(i)[1]);
			if(i < list22.size()-1) System.out.print("+");
		}
		System.out.println();
	}
	
	
	//相加
	@SuppressWarnings("unchecked")
	public static LinkedList<int[]> add(LinkedList<int[]> list1, LinkedList<int[]> list2){
		int[] xc = new int[2];
		xc[0] = 100;
		xc[1] = 100;
		LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone();
		for(int i = 0; i < list1.size(); i++){
			for(int j = 0; j < temp.size(); j++){
				if(temp.get(j)[1] == list1.get(i)[1]) {
					System.out.print("系数一样");
				list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0];
				temp.set(j, xc);
				}
			}
		}
		for(int i = 0; i < temp.size(); i++){
			if(temp.get(i)[0] == 100) {
				temp.remove(i);
				i--;
			}
		}
		list1.addAll(temp);

		return temp;
	}
	
	//相减
	@SuppressWarnings("unchecked")
	public static LinkedList<int[]> min(LinkedList<int[]> list1, LinkedList<int[]> list2){
		int[] xc = new int[2];
		xc[0] = 100;
		xc[1] = 100;
		LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone();   //这里我又复制了一份,所以最开始的list2理论上是不会改变的,无论怎么操作。
		for(int i = 0; i < temp.size(); i++){
			int[] x =new int[2];
			x[0] = -temp.get(i)[0];
			x[1] = temp.get(i)[1];
			temp.set(i, x);
		}
		for(int i = 0; i < list1.size(); i++){
			for(int j = 0; j < temp.size(); j++){
				if(temp.get(j)[1] == list1.get(i)[1]){ 
				list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0];
				
				temp.set(j, xc);
				}
			}
		}
		for(int i = 0; i < temp.size(); i++){
			if(temp.get(i)[1] == 100) {
				temp.remove(i);
				i--;
			}
		}
		list1.addAll(temp);
		
		
		return temp;
	}
}
GUILTYxc 2018-04-01
  • 打赏
  • 举报
回复
import java.util.LinkedList; import java.util.Scanner; public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); //T组测试数据 for(int i = 0; i < T; i++){ LinkedList<int[]> list1 = new LinkedList<>(); LinkedList<int[]> list2 = new LinkedList<>(); int f = sc.nextInt(); //第一个多项式的长度 int s = sc.nextInt(); //第二个的 //给多项式赋值,分别是系数和指数 int ii = 0; while(ii < f){ int[] po = new int[2]; po[0] = sc.nextInt(); po[1] = sc.nextInt(); list1.add(po); ii++; } for(int lp = 0; lp < s ;lp++){ int[] pi = new int[2]; pi[0] = sc.nextInt(); pi[1] = sc.nextInt(); list2.add(pi); } System.out.println(list1.size()); //输出一下多项式的长度 System.out.println(list2.size()); //把list2复制出来一份,以后除了输出都对temp操作,不改变list2的值 LinkedList<int[]> temp = new LinkedList<>(); temp = (LinkedList<int[]>) list2.clone(); int q = sc.nextInt(); //一共几次操作,其中1代表输出,2多项式相加结果存到list1中,3相减 for(int iii = 0; iii < q; iii++){ int qq = sc.nextInt(); if(qq == 1) mylist.outPut(list1, list2); else if(qq == 2) mylist.add(list1, temp); else if(qq == 3) mylist.min(list1, temp); } } } } class mylist{ //输出函数的实现 public static void outPut(LinkedList<int[]> list11, LinkedList<int[]> list22){ for(int i = 0;i < list11.size(); i++){ if(list11.get(i)[0] > 0&&i > 0&&list11.get(i)[0] != 0&&list11.get(i)[0] != 100 ) System.out.print("+"); if(list11.get(i)[0] != 0&&list11.get(i)[0] != 100 ) System.out.print(list11.get(i)[0]+"X^"+list11.get(i)[1]); } System.out.println(); for(int i = 0; i < list22.size(); i++){ System.out.print(list22.get(i)[0]+"X^"+list22.get(i)[1]); if(i < list22.size()-1) System.out.print("+"); } System.out.println(); } //相加 @SuppressWarnings("unchecked") public static LinkedList<int[]> add(LinkedList<int[]> list1, LinkedList<int[]> list2){ int[] xc = new int[2]; xc[0] = 100; xc[1] = 100; LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone(); for(int i = 0; i < list1.size(); i++){ for(int j = 0; j < temp.size(); j++){ if(temp.get(j)[1] == list1.get(i)[1]) { System.out.print("系数一样"); list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0]; temp.set(j, xc); } } } for(int i = 0; i < temp.size(); i++){ if(temp.get(i)[0] == 100) { temp.remove(i); i--; } } list1.addAll(temp); return temp; } //相减 @SuppressWarnings("unchecked") public static LinkedList<int[]> min(LinkedList<int[]> list1, LinkedList<int[]> list2){ int[] xc = new int[2]; xc[0] = 100; xc[1] = 100; LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone(); //这里我又复制了一份,所以最开始的list2理论上是不会改变的,无论怎么操作。 for(int i = 0; i < temp.size(); i++){ int[] x =new int[2]; x[0] = -temp.get(i)[0]; x[1] = temp.get(i)[1]; temp.set(i, x); } for(int i = 0; i < list1.size(); i++){ for(int j = 0; j < temp.size(); j++){ if(temp.get(j)[1] == list1.get(i)[1]){ list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0]; temp.set(j, xc); } } } for(int i = 0; i < temp.size(); i++){ if(temp.get(i)[1] == 100) { temp.remove(i); i--; } } list1.addAll(temp); return temp; } }
GUILTYxc 2018-04-01
  • 打赏
  • 举报
回复
我在实际应用中每次操作复制品,原数据总会发生改变,怎么改都没用,弄不明白是怎么回事,,绝望啊!!!
young_ao 2018-04-01
  • 打赏
  • 举报
回复

LinkedList<int[]> list = new LinkedList<>();
LinkedList<int[]> copyList = (LinkedList<int[]>) list.clone();
System.err.println(list == copyList);

// 附上源码
/**
     * Returns a shallow copy of this {@code LinkedList}. (The elements
     * themselves are not cloned.)
     *
     * @return a shallow copy of this {@code LinkedList} instance
     */
    public Object clone() {
        LinkedList<E> clone = superClone();

        // Put clone into "virgin" state
        clone.first = clone.last = null;
        clone.size = 0;
        clone.modCount = 0;

        // Initialize clone with our elements
        for (Node<E> x = first; x != null; x = x.next)
            clone.add(x.item);

        return clone;
    }
GUILTYxc 2018-04-01
  • 打赏
  • 举报
回复
引用 1 楼 Lone1yCode 的回复:
不会,克隆和直接赋值不一样,克隆会另开一块内存,直接赋值就是两个List指向同一个内存地址。
那这样的语法对吗? LinkedList<int[]> list = new LinkedList<>(); LInkedList<int[]> copy = newLinkedList<>(); copy = (LInkedList<int[]>) list.clone();
__椎名真白 2018-04-01
  • 打赏
  • 举报
回复
不会,克隆和直接赋值不一样,克隆会另开一块内存,直接赋值就是两个List指向同一个内存地址。

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧