java - Why does the javac error "(x) cannot be applied to (y)", happen when both parameters and arguments match up? (inner-class calling outer-class method) -
learning java iterators , general data structures via means of homework.
i have built doubly-linked list (linkedlist) uses nodes (linkedlist$node) , has iterator (linkedlist$linkedlistiterator) classes make use of generics.
within linkedlistiterator's @overridden remove() method making use of method of outer-class, i.e., linkedlist class.
i following compile-time error:
./linkedlist.java:170: deletenode(linkedlist<t>.node<t>,linkedlist<t>.node<t>,linkedlist<t>.node<t>) in linkedlist<t> cannot applied (linkedlist<t>.node<t>,linkedlist<t>.node<t>,linkedlist<t>.node<t>) deletenode(nodetoberemoved, next, prev);
my (primitive) understanding types don't match up, cannot see how so.
here complete class code:
import java.util.iterator; import java.util.nosuchelementexception; import java.util.concurrentmodificationexception; public class linkedlist<t> implements iterable<t> { private node<t> sentinel; private long modcount; //apparently no unsigned int's in java. public linkedlist() { modcount = 0; sentinel = new node<t>(null); sentinel.setnext(sentinel); sentinel.setprev(sentinel); } public void append(t t) { /* append: ... [-----] | | [-1st-] | | infront-> [sentl] | | <-- [newnode] behind--> [-----] | | [-----] ... */ node<t> newnode = new node<t>(t); node<t> infront = sentinel; node<t> behind = sentinel.prev(); //now insert: insertnode(newnode, infront, behind); } public void prepend(t t) { /* prepend: ... [-----] | | infront-> [-1st-] | | <-- [newnode] behind--> [sentl] | | [-----] | | [-----] */ node<t> newnode = new node<t>(t); node<t> behind = sentinel; node<t> infront = sentinel.next(); //now insert: insertnode(newnode, infront, behind); } public void removehead() { /* remove-first: ... infront --> [-----] | | [-1st-] <-- *delete* | | behind ---> [sentl] | | [-----] | | [-----] ... */ node<t> infront = sentinel.next().next(); node<t> behind = sentinel; node<t> todelete = sentinel.next(); // delete deletenode(todelete, infront, behind); } private void insertnode(node<t> newnode, node<t> infront, node<t> behind) { newnode.setnext(infront); newnode.setprev(behind); infront.setprev(newnode); behind.setnext(newnode); modcount++; } private void deletenode(node<t> todelete, node<t> infront, node<t> behind) { infront.setprev(behind); behind.setnext(infront); todelete.setnext(null); todelete.setprev(null); modcount++; } @override public iterator<t> iterator() { return new linkedlistiterator<t>(sentinel); } /* ..:: myiterator ::.. private inner class */ public class linkedlistiterator<t> implements iterator<t> { private node<t> cursor; private node<t> lastreturned; private long itermodcountperspective; public linkedlistiterator(node<t> sentinel) { cursor = sentinel.next(); lastreturned = null; itermodcountperspective = modcount; } private boolean hasbodhi() { // bodhi: in buddhism, bodhi understanding of "true nature of things". return (itermodcountperspective == modcount); } @override public boolean hasnext() { if (cursor == sentinel) return false; return true; } @override public t next() { if (!this.hasnext()) { throw new nosuchelementexception(); } else if (!hasbodhi()) { throw new concurrentmodificationexception(); } else { t aux = cursor.data(); lastreturned = cursor; cursor = cursor.next(); return aux; } } @override public void remove() { //check we're allowed remove: if (lastreturned == null) { throw new illegalstateexception(); } if (!hasbodhi()) { throw new concurrentmodificationexception(); } //setup vars perform deletion: node<t> nodetoberemoved = lastreturned; node<t> next = nodetoberemoved.next(); node<t> prev = nodetoberemoved.prev(); // delete deletenode(nodetoberemoved, next, prev); itermodcountperspective++; //now setup vars exit: cursor = next; lastreturned = null; // illegal remove yet-again before first calling next() } } /* ..:: node ::.. private, compositional inner class interface: void setnext(node n) // change node in front of node void setprev(node p) // change node behind node node next() // returns node in front of node node prev() // returns node behind node t data() // returns data stored inside node */ private class node<t> { private t data; private node<t> next; private node<t> prev; public node(t d) { data = d; next = null; prev = null; } /* method setnext(node<t> n) method takes parameter node passed-in , puts in front of node. input - node n output - none eg: node4.setnext(node5); */ public void setnext(node<t> n) { next = n; } /* method setprev(node<t> n) method takes parameter node passed-in , puts behind of node. input - node p output - none eg: node5.setprev(node4); */ public void setprev(node<t> p) { prev = p; } /* method next() method returns node in front of node. input - none output - node infront of (this.next) eg: node nodeinfrontofnode4 = node4.next(); */ public node<t> next() { return next; } /* method prev() method returns node behind of node. input - none output - node behind of (this.prev) eg: node nodebehindofnode4 = node4.prev(); */ public node<t> prev() { return prev; } /* method data() method returns data inside of node. input - none output - data inside of node eg: planarshape shape4 = node4.data(); */ public t data() { return data; } } }
comp-sci student, first time poster. thank gurus. you've helped peers , many times
that happening, because you're defining 3 different type parameters there. whenever declare class as:
class x<t> {}
... define new type parameter t
, whether inner class or outer class. so, in below scenario:
class x<t> { class y<t> { } }
both t's
different. can this:
x<string> x = new x<string>(); y<double> y = x.new y<double>();
so, consider case here:
class x<t> { public void hellothere(t obj) { } class y<t> { // ignore syntactical error method calling. public void callhellothere(t obj) { hellothere(obj); } } }
that not work for:
y.callhellothere(2.5);
because then, passing double
type method accepts string
, since instantiated x<string>
.
this same scenario you're facing in example.
solution? change class y<t>
class y
, , you're set.
Comments
Post a Comment