notToday
notToday

Reputation: 13

LinkedList got same value while I printed the list even tough i entered different values

I was running the below code

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);
        }

    }

}

and i got the output:

1
1
5

I think the output should be 1 4 5 but it is 1 1 5. I don't what's the mistake in my code.

Upvotes: 0

Views: 32

Answers (2)

Mahesh
Mahesh

Reputation: 21

Hey I have modified your code to get the desired output.

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);
        }

    }

}

The variable d is basically reference to the object, hence any modification you do using the reference d will modify the same object and the changes will get reflected in the list

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58774

You added d twice to list and you change its roll value to 1

It effects object properties even it d was already added to list before

For expected results add a new instance instead of d:

link.add(new CollectionAndClass(1, 5));

Upvotes: 1

Related Questions