"String cannot be converted to PubliclyCloneable" Java Issue -
so trying add few strings doubly linked list keep getting error string cannot converted publiclycloneable (an interface extends cloneable, use purposes of creating clone() , copy constructor, la textbook's explanation). here demo code looks like:
public static void main(string[] args) { doublylinkedlist list1 = new doublylinkedlist(); doublylinkedlist.doublylinkediterator = list1.iterator(); string pittsburgh1 = new string("penguins"); string pittsburgh2 = new string("pirates"); string pittsburgh3 = new string("steelers"); list1.addtostart(pittsburgh1); list1.addtostart(pittsburgh2); list1.addtostart(pittsburgh3); system.out.println("list contains:"); i.restart(); while (i.hasnext()) system.out.println(i.next()); system.out.println("");
here beginning part of class doublylinkedlist:
public class doublylinkedlist<t extends publiclycloneable> implements publiclycloneable { private class twowaynode<t> { private t data; private twowaynode<t> next; private twowaynode<t> prev; public twowaynode() { data = null; next = null; prev = null; } public twowaynode(t newdata, twowaynode<t> prevnode, twowaynode<t> nextnode) { data = newdata; next = nextnode; prev = prevnode; } }
here method causing compiler error:
public void addtostart(t itemdata) { twowaynode<t> newhead = new twowaynode(itemdata, null, head); if (head != null) { head.prev = newhead; } head = newhead; }
and here interface using:
public interface publiclycloneable extends cloneable { public object clone(); }
so there overlooking here, trying accomplish possible? there sort of typecast or type assignment can add 3 strings list?
your addtostart
method accepts argument of type t
, t
defined class t extends publiclycloneable
.
since java.lang.string
not implement publiclycloneable
, cannot argument type doublylinkedlist
. if want main
function test class, i'd implement simple-stupid publiclyclonablestring
class::
public class publiclyclonablestring implements publiclyclonable { private string s; public publiclyclonablestring (string s) { this.s = s; } @override public string clone() { return s; // ok, since strings immutable } // getter, setter, etc. }
Comments
Post a Comment