Sunday 15 June 2014

How to use Java generics to avoid casting? -



How to use Java generics to avoid casting? -

for query, raised in link, java generics recommended avoid difficulty in assessing run time type of item.

after using java generics in below code, not see incompatible type errors unlike before.

but different compile time error @ line 96 , dlist1<int> l = new dlist1<int>(); not give clue of problem.

error message is: syntax error on token 'int'

/* dlist1.java */ /** * dlist1 mutable doubly-linked list. (no sentinel, not * circularly linked.) */ public class dlist1<t> { /** * head references first node. * tail references lastly node. * * not alter next field declarations. */ protected dlistnode1<t> head; protected dlistnode1<t> 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(t item) { if(this.head == null){ this.head = new dlistnode1<t>(item); this.tail = this.head; }else{ dlistnode1<t> newnode = new dlistnode1<t>(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<t> 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<int> l = new dlist1<int>(); //line 96 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 104 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.item != 9) { system.out.println("tail.item wrong."); }*/ 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."); } } /* end main() */ } /* dlistnode1.java */ /** * dlistnode1 node in dlist1 (doubly-linked list). */ class dlistnode1<t> { /** * item references item stored in current node. * prev references previous node in dlist. * next references next node in dlist. * * not alter next field declarations. */ t item; dlistnode1<t> prev; dlistnode1<t> next; /** * dlistnode1() constructor. */ dlistnode1() { this.item = null; this.prev = null; this.next = null; } dlistnode1(t item) { this.item = item; this.prev = null; this.next = null; } }

my question:

what meaning of error @ line 96? how resolve this?

note: using jre 1.6 update 45

in java can't parametrize type primitive values int, instead of:

dlist1<int> l = new dlist1<int>();

use wrapper classes:

dlist1<integer> l = new dlist1<integer>();

java generics

No comments:

Post a Comment