Passing object in Dlist.

The line "if (l.head.item != 9" gave me the error it said something like object is not compatible with int. I am really confused on why is that? How to fix it?

/DListNode1/

/* DListNode1.java */

public class DListNode1 {


  public Object item;
//  public short[][] colorVal;
  public DListNode1 prev;
  public DListNode1 next;


  DListNode1() {
    item = 0;
    prev = null;
    next = null;
  }

  DListNode1(Object i) {
    item = i;
    prev = null;
    next = null;
  }
}
//////////////
  /* Double linked list */
public class DList1 {

  protected DListNode1 head;
  protected DListNode1 tail;
  protected long size;

  public DList1() {
    head = null;
    tail = null;
    size = 0;
  }



  public DList1(Object a) {
    head = new DListNode1();
    tail = head;
    head.item = a;
    size = 1;
  }  
  public DList1(Object a, Object b) {
    head = new DListNode1();
    head.item = a;
    tail = new DListNode1();
    tail.item = b;
    head.next = tail;
    tail.prev = head;
    size = 2;
  }

  public void insertFront(Object i) {
    DListNode1 temp = new DListNode1(i);
    if (size == 0) {
      head = temp;
      tail = temp;
    }
    else {
      temp.next = head;
      head.prev = temp;
      head = temp;
    } size++;  
  }


  public void removeFront() {
    if (size == 0) {
      return;
    }
    else if (size == 1) {
      head = null;
      tail = null;
      size--;
    }
    else {
      head = head.next;
      head.prev = null;
      size--;
    }
  }

  public String toString() {
    String result = "[  ";
    DListNode1 current = head;
    while (current != null) {
      result = result + current.item + "  ";
      current = current.next;
    }
    return result + "]";
  }
/////////////

 public static void main(String[] args) {


    DList1 l = new DList1();
    l.insertFront(9);
     if (l.head.item != 9) {
        System.out.println("head.item is wrong.");
Jon Skeet
people
quotationmark

As others have pointed out, the problem is that the type of l.head.item is Object, and you can't compare that with an int using != or ==.

Options:

  • Cast l.head.item to Integer or int:

    // This could be done in one step if you wanted
    int headValue = (int) l.head.item;
    if (headValue != 9)
    

    Or

    // This could be done in one step if you wanted
    Integer headValue = (Integer) l.head.item;
    if (headValue != 9)
    

    Or you could do it inline:

    if ((int) l.head.item != 9)
    
  • Use equals instead, which will automatically box the int to an Integer.

    if (!head.equals(9))
    
  • Make your type generic instead, so you'd have a DListNode1<Integer>, and you could then be certain that all node values were Integer references (or null), and the != check would automatically unbox the Integer to an int and work.

Personally I'd definitely make this generic, but my guess is that you're relatively new to Java, and might not want to start with generics just yet.

Note that there's a difference between using equals and performing the unboxing: if the value of l.head.item is a reference to a non-Integer object, the first approach will throw a ClassCastException and the second will just go into the body of the if statement (as a string is not equal to 9, for example). Which of those is preferable depends on what you're trying to achieve in your bigger program: if it's entirely reasonable for your list to contain non-integers, you should use the equals check; if it actually indicates a programming error, then an exception is preferable as it alerts you to the error more quickly and stops your program from proceeding with invalid assumptions.

In both cases if l.head.item is null, you'll get a NullPointerException. This could be "fixed" using:

if (!Integer.valueOf(9).equals(l.head.item))

... but again it depends on what you want your code to do if the value is null.

people

See more on this question at Stackoverflow