Java LinkedList在我打印列表时得到了相同的值,尽管我输入了不同的值

Java LinkedList在我打印列表时得到了相同的值,尽管我输入了不同的值,java,collections,Java,Collections,我正在运行下面的代码 import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; class Ca implements Comparator<CollectionAndClass>{ public int compare(CollectionAndClass a,CollectionAndClass b){

我正在运行下面的代码

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
class Ca implements Comparator<CollectionAndClass>{
    public int compare(CollectionAndClass a,CollectionAndClass b){
        return a.roll-b.roll;   
    }

}
public class CollectionAndClass {
    int roll;
    int dar;
    public CollectionAndClass(int a,int b) {
        roll=a;
        dar=b;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedList<CollectionAndClass> link=new LinkedList<CollectionAndClass>();
        CollectionAndClass d=new CollectionAndClass(4,5);
        link.add(d);
        link.add(new CollectionAndClass(5,6));
        d.roll=d.dar=1;
        link.add(d);
        Collections.sort(link, new Ca());
        Iterator<CollectionAndClass> itr=link.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next().roll);
        }

    }

}
我认为输出应该是1 4 5,但它是1 1 5。我不知道我的代码有什么错误

您在列表中添加了两次d,并将其滚动值更改为1

它影响对象属性,即使它以前已添加到列表中

为获得预期结果,请添加一个新实例,而不是d:


嘿,我已经修改了你的代码以获得所需的输出

package com.dream11.contest;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
class Ca implements Comparator<CollectionAndClass>{
    public int compare(CollectionAndClass a,CollectionAndClass b){
        return a.roll-b.roll;
    }

}
public class CollectionAndClass {
    int roll;
    int dar;
    public CollectionAndClass(int a,int b) {
        roll=a;
        dar=b;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedList<CollectionAndClass> link=new LinkedList<CollectionAndClass>();
        CollectionAndClass d=new CollectionAndClass(4,5);
        link.add(d);
        link.add(new CollectionAndClass(5,6));

        //d.roll=d.dar=1; You are modifying the same object d, it is reference to object added in list, hence any modification via d will also reflect in the list
        //        link.add(d); and adding same object again
        CollectionAndClass d2=new CollectionAndClass(1,1); // instead create a new object , with new set of values
        link.add(d2); // add in the list
        Collections.sort(link, new Ca());
        Iterator<CollectionAndClass> itr=link.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next().roll);
        }

    }

}

变量d基本上是对对象的引用,因此使用引用d进行的任何修改都将修改同一对象,并且更改将反映在列表中

link.add(new CollectionAndClass(1, 5));
package com.dream11.contest;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
class Ca implements Comparator<CollectionAndClass>{
    public int compare(CollectionAndClass a,CollectionAndClass b){
        return a.roll-b.roll;
    }

}
public class CollectionAndClass {
    int roll;
    int dar;
    public CollectionAndClass(int a,int b) {
        roll=a;
        dar=b;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LinkedList<CollectionAndClass> link=new LinkedList<CollectionAndClass>();
        CollectionAndClass d=new CollectionAndClass(4,5);
        link.add(d);
        link.add(new CollectionAndClass(5,6));

        //d.roll=d.dar=1; You are modifying the same object d, it is reference to object added in list, hence any modification via d will also reflect in the list
        //        link.add(d); and adding same object again
        CollectionAndClass d2=new CollectionAndClass(1,1); // instead create a new object , with new set of values
        link.add(d2); // add in the list
        Collections.sort(link, new Ca());
        Iterator<CollectionAndClass> itr=link.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next().roll);
        }

    }

}