Wednesday 15 February 2012

How implement Java Generics -



How implement Java Generics -

i think set class right generic when seek phone call methods cant seem set other methods correct. im supposed cast variables generic? or cast methods variables?

public class linkedlist<e> { // reference head node. private e head; private int listcount; public boolean delete(e string) // post: removes element @ specified position in list. { node current = head.getnext(); while(true){ if(current == null){ homecoming false; }else if(current.getnext().getdata().equals(string)){ if(current.getnext() == null){ current.setnext(null); }else{ current.setnext(current.getnext().getnext()); } listcount--; // decrement number of elements variable homecoming true; }else{ current = current.getnext(); } } } private class node<e extends comparable<e>> { // reference next node in chain, e next; // info carried node. // of type need. e data; // node constructor public node(e _data) { next = null; info = _data; } // node constructor if want // specify node point to. public node(e _data, e _next) { next = _next; info = _data; } // these methods should self-explanatory public e getdata() { homecoming data; } public void setdata(e _data) { info = _data; } public e getnext() { homecoming next; } public void setnext(e _next) { next = _next; } } }

the types of variables bit messed up.

node.next needs node linkedlist.head needs node node not need generic. (the e type parameter in scope inner class.)

here's version compiles:

class linkedlist<e> { // reference head node. private node head; private int listcount; public boolean delete(e string) // post: removes element @ specified position in list. { node current = head; while (true) { if (current == null) { homecoming false; } else if (current.getdata().equals(string)) { if (current.getnext() == null) { current.setnext(null); } else { current.setnext(current.getnext().getnext()); } listcount--; // decrement number of elements variable homecoming true; } else { current = current.getnext(); } } } private class node { // reference next node in chain, node next; // info carried node. // of type need. e data; // node constructor public node(e _data) { next = null; info = _data; } // node constructor if want // specify node point to. public node(e _data, node _next) { next = _next; info = _data; } // these methods should self-explanatory public e getdata() { homecoming data; } public void setdata(e _data) { info = _data; } public node getnext() { homecoming next; } public void setnext(node _next) { next = _next; } } }

looking @ delete method, think it's bit buggy though. when arrive @ node data equals string, alter next-pointer of that node while should changing next-pointer of previous node.

i seek this:

node current = head, prev = null; while (current != null) { if (current.getdata().equals(string)) { // remove current list if (current == head) { head = current.getnext(); } else { prev.setnext(current.getnext()); } listcount--; // decrement number of elements variable homecoming true; } prev = current; current = current.getnext(); }

java generics

No comments:

Post a Comment