0

I have the following code, how can I use the compareTo method with primitive types:

public class mySortertedList<T extends Comparable<T>> extends myLinkedList<T> {
    
    class Node {
    Node next = null;
    T data;
    Node (T x) {
        data = x;
    }
}


public int compareTo (T Obj) {
        return this.compareTo(Obj);
}

Basically what I have is a sorted linked list which adds values to the list based on the compareTo method. How can I make sure that the compareTo works for primitive types, at least Integers? Any help is highly appreciated!

EDIT: To clarify, here is more code: The main programme :

public static void main(String[] args) {
    newList.addTo(4);
    newList.addTo(1337);
    newList.addTo(30);
    newList.addTo(15);
}

I also have other methods like get(int index), remove() i.e. removes the last element in the list. Here is the addTo method from mySortertList<T extends Comparable> class, there is a declaration of variable start of type Node, it is really just a pointer as a starting point to go through the whole linked list one by one:

private Node start = null;

public void addTo(T x) {
        Node newNode = new Node(x);
        Node pointer = start;
        if (start == null) {
            start = newNode;
        } else if (size()==1){
            if (newNode.data.compareTo(start.data)<0) {
                newNode.next= start;
                start = newNode;
            } else {
                start.next= newNode;
            }
        } else {
    

            boolean valuePlaced= false;
            Node pointer2= start.next;
    
            while (pointer2 !=null) {
                if (newNode.data.compareTo(pointer.data)<0) {
                    newNode.next= start.next;
                    start.next = newNode;
                    valuePlaced = true;
                } else if (newNode.data.compareTo(pointer2.data)<0) {
                    pointer = newNode;
                    new.next= pointer2;
                    valuePlaced = true;
                }
                pointer = pointer.next;
                pointer2 = pointer2.next;
            }
            
            if (valuePlaced == false) {
                pointer.next= newNode;
            }
        }
    }
Akhil
  • 923
  • 2
  • 7
  • 10
  • 1) where _exactly_ is your primitive here 2) do not compare strings with `==` 3) this question makes little sense – Eugene Mar 12 '21 at 17:40
  • Eugene, when I make a list in the main Program, I make it like mySortedList, and I pass in int values to the compareTo method, like this.data.compareTo(x.data), but since the compareTo only works on classes, not primitives, it won't work. – Akhil Mar 12 '21 at 17:43
  • Also, that if statement where I use == to compare String is because I know exactly what (Object)Obj.getClass().getName() is will be if Obj is an instanceof the primitive type int. I use this because I cannot called instanceof int on a generic object. – Akhil Mar 12 '21 at 17:45
  • Could you provide us a few lines of code where you (1) declare the list of integers, (2) implements the method compareTo ? Here it seems it is in Node class or mySortertedList class. It is not clear enough. – g.momo Mar 12 '21 at 18:28
  • g.momo just added the main programme and the addTo method – Akhil Mar 12 '21 at 19:04

2 Answers2

1

I don't know whether I got you well, but this is what I can suggest you :

  • assuming compareTo() is in class Node... (**)
  • I tested with LinkedList<T> instead of your myLinkedList<T>

The following works correctly at my ends.


// we are in file Test.java
public class Test { 
   public static void main(String... args) {
        mySortertedList<Integer> newList = new mySortertedList<>();
        newList.addTo(4);
        newList.addTo(1337);
        System.out.println("newList "+ newList.toString()); // newList , 4, 1337
        newList.addTo(30);
        System.out.println("newList "+ newList.toString()); // newList , 4, 30, 1337
        newList.addTo(15);
        System.out.println("newList "+ newList.toString()); // newList , 4, 15, 30, 1337
    }
}
class mySortertedList<T extends Comparable<T>> extends LinkedList<T> { // rather than your myLinkedList<T>

    private Node start = null;

    class Node {
        Node next = null;
        T data;
        Node(T x) {
            data = x;
        }

        public int compareTo(T Obj) { // (**)
            if ((Object) Obj.getClass().getName() == "java.lang.Integer") {
                return Integer.compare((Integer) this.data, (Integer) Obj);
            } else {
                return this.compareTo(Obj);
            }
        }
    }

    public String toString() {
        String s = "";
        Node pointer2= start;

        while (pointer2 !=null) {
            s += ", "+ pointer2.data.toString();
            pointer2 = pointer2.next;
        }
        return s;
    }

    public void addTo(T x) {
        Node newNode = new Node(x);
        Node pointer = start;
        if (start == null) {
            start = newNode;
        } else if (start.next == null) { // (size()==1){ 
            if (newNode.data.compareTo(start.data)<0) {
                newNode.next= start;
                start = newNode;
            } else {
                start.next= newNode;
            }
        } else {
            boolean valuePlaced= false;
            if (newNode.data.compareTo(start.data)<0) {
                newNode.next= start;
                start = newNode;
            }
            else {
                while (valuePlaced == false && pointer.next != null) {
                    if (newNode.data.compareTo(pointer.next.data) < 0) {
                        newNode.next = pointer.next;
                        pointer.next = newNode;
                        valuePlaced = true;
                        break;
                    }
                    pointer = pointer.next;
                }

                if (valuePlaced == false) {
                    pointer.next = newNode;
                }
            }
        }
    }

}

To test, just create a Test.java and copy paste this code.

g.momo
  • 536
  • 2
  • 7
0

Just use compareTo for all cases.

Integer is a class, not a primitive. int is a primitive, but it can't appear in generics. You don't need to do anything special whatsoever, and can just use compareTo, which works fine on Integer.

It is impossible for obj to be a primitive (in current versions of Java). obj will always be an object.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Louis Wasserman, I have list composed of int types. Now to add the int values, I want to use compareTo everytime a new int value is added to the list i.e. the larger int values go to the back of the list. Problem is I cannot cast int into a Integer (gives error: The constructor Integer(T) is not defined), neither can I pass in int values into the compareTo function because I get error int cannot be deferenced. I know Obj is always object, but I also want to pass in Integer values, for that I need that the int value I pass in is either converted to Integer class. – Akhil Mar 12 '21 at 18:14