Question on java

Feb 20, 2014 at 6:42am


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*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.");


The line "if (l.head.item != 9" gave me the error it said something like object is not compatible with int. I am really confuse on why is that.
Last edited on Feb 20, 2014 at 6:45am
Feb 20, 2014 at 7:21am
this should a) be in the lounge b) be in the proper forum. does head have the member item?
Feb 20, 2014 at 9:23am
l.head.item, lets look at the classes of that construct: Dlist1.DListNode1.Object
Yeah, that's not an int...

You could either implement item as an int, or cast it to an int. Is there a reason you're using an Object class for item?
Topic archived. No new replies allowed.