java - How to compare objects with different types? -
in below program, dlist1 list abstraction.
i run unit test cases in main() function of dlist1 class.
to specific,
in main() method @ line 105, l.head.item != 9 , gives compile error: incompatible operand types object , int.
at compile time, value 9 of primitive type int , l.head.item of type class object.
at run time, l.head.item of type class integer. not sure type of value 9.
/* dlist1.java */ /** * dlist1 mutable doubly-linked list. (no sentinel, not * circularly linked.) */ public class dlist1 { /** * head references first node. * tail references lastly node. * * not alter next field declarations. */ protected dlistnode1 head; protected dlistnode1 tail; protected long size; /* dlist1 invariants: * 1) head.prev == null. * 2) tail.next == null. * 3) dlistnode1 x in dlist, if x.next == y , x.next != null, * y.prev == x. * 4) dlistnode1 x in dlist, if x.prev == y , x.prev != null, * y.next == x. * 5) tail can accessed head sequence of "next" * references. * 6) size number of dlistnode1s can accessed * head sequence of "next" references. */ /** * dlist1() constructor empty dlist1. */ public dlist1() { this.head = null; this.tail = null; this.size = 0; } /** * insertfront() inserts item @ front end of dlist1. */ public void insertfront(object item) { if(this.head == null){ this.head = new dlistnode1(item); this.tail = this.head; }else{ dlistnode1 newnode = new dlistnode1(item); newnode.next = this.head; this.head.prev = newnode; this.head = newnode; } this.size++; } /** * removefront() removes first item (and node) dlist1. if * list empty, nothing. */ public void removefront() { if(this.size == 0){ return; }else if(size ==1){ this.head = null; this.tail = null; }else{ this.head.next.prev = null; this.head = this.head.next; } } /** * tostring() returns string representation of dlist. * * not alter method. * * @return string representation of dlist. */ public string tostring() { string result = "[ "; dlistnode1 current = head; while (current != null) { result = result + current.item + " "; current = current.next; } homecoming result + "]"; } public static void main(string[] args) { // not alter next code. dlist1 l = new dlist1(); system.out.println("### testing insertfront ###\nempty list " + l); l.insertfront(9); system.out.println("\ninserting 9 @ front.\nlist 9 " + l); if (l.head == null) { system.out.println("head null."); } else { if (l.head.item != 9) { //line 105 system.out.println("head.item wrong."); } if (l.head.prev != null) { system.out.println("head.prev wrong."); } } if (l.tail == null) { system.out.println("tail null."); } else { /*if (l.tail.item != 9) { system.out.println("tail.item wrong."); } if (l.tail.next != null) { system.out.println("tail.next wrong."); }*/ } if (l.size != 1) { system.out.println("size wrong."); } l.insertfront(8); system.out.println("\ninserting 8 @ front.\nlist 8 , 9 " + l); if (l.head == null) { system.out.println("head null."); } else { /*if (l.head.item != 8) { system.out.println("head.item wrong."); }*/ if (l.head.prev != null) { system.out.println("head.prev wrong."); } if (l.head.next != l.tail) { system.out.println("head.next wrong."); } } if (l.tail == null) { system.out.println("tail null."); } else { if (l.tail.next != null) { system.out.println("tail.next wrong."); } if (l.tail.prev != l.head) { system.out.println("tail.prev wrong."); } } if (l.size != 2) { system.out.println("size wrong."); } } } /* dlistnode1.java */ /** * dlistnode1 node in dlist1 (doubly-linked list). */ class dlistnode1 { /** * item references item stored in current node. * prev references previous node in dlist. * next references next node in dlist. * * not alter next field declarations. */ object item; dlistnode1 prev; dlistnode1 next; /** * dlistnode1() constructor. */ dlistnode1() { this.item = null; this.prev = null; this.next = null; } dlistnode1(object item) { this.item = item; this.prev = null; this.next = null; } } my question:
how can create types compatible both @ compile time , run time , allow value compared in line 105? run time type of value 9?
you can utilize type cast before comparision: (int)l.head.item != 9(it works in java 7 , later, in older versions can utilize (integer)l.head.item). if list supposed homogeneous, using generics idea.
java
No comments:
Post a Comment