Java- Error concerning Generics -



Java- Error concerning Generics -

i have class & , inner class work create linked list. info held nodes, generic, , somewhere along line of code screwed involving generics don't know where. 5 errors, first 1 says "type linkedlist not take parameters" think weird because linkedlist interface i'm implementing. rest of them phrased "object cannot converted e". marked they're occurring. figured assignment right don't know what's wrong. if you'd me post interface linkedlist allow me know.

public class singlylinkedlist<e> implements linkedlist<e> { private node head; private node tail; private int size; public singlylinkedlist(){ this.head = null; this.tail = null; this.size = 0; } public e get(int index) { //hold info of cur node //and maintain iterating until nail //right index int nail = 0; node pos = this.head; e found = this.head.data;//////error here while (hit < index) { pos = pos.next; found = pos.data;//////error here hit++; } homecoming found; } public void add together (e data) { node lastly = new node(data, null); //if list empty. if (this.head == null) { this.head = last; } //make cur tail's next new node //and set new node new tail. this.tail.next = last; this.tail = last; this.size++; } public boolean add(int index, e data) { //cannot add together if index not valid if ((index >= this.size) || (index < 0) ) { homecoming false; } //if index 0, add together @ origin node insert = new node(data, null); if (index == 0) { insert.next = this.head; this.head = insert; this.size++; homecoming true; } else { //else, go until reach desired index //set whatever @ index new node's //next value. int nail = 1; node cur = this.head; while (hit < index) { cur = cur.next; hit++; } insert.next = cur; this.size++; homecoming false; } } public void addall(collection<? extends e> c) { (e item: c) { this.add(item); } } public boolean contains(e data) { int nail = 0; node cur = this.head; node move; while (hit < this.size) { if (data.equals(cur.data)) { homecoming true; } else { move = cur.next; cur = move; } } homecoming false; } public boolean containsall(collection<? extends e> c) { (e item: c) { if (!(this.contains(item))) { homecoming false; } } homecoming true; } public boolean remove(e data) { node prev = this.head; node cur = this.head.next; int nail = 1; if (!(this.contains(data))) { homecoming false; } else if (data.equals(head.data)) { this.head = cur; homecoming true; this.size--; } while (hit < this.size) { if (data.equals(cur.data)) { prev.next = cur.next; homecoming true; this.size--; } hit++; } } public e remove(int index) { int nail = 1; e data; node prev = this.head; node cur = this.head.next; if (index == 0) { info = head.data; //////error here this.head = cur; homecoming data; this.size--; } else { while (hit < this.size) { if ( nail == index) { info = cur.data; //////error here prev.next = cur.next; homecoming data; this.size--; } else { prev = cur; cur = prev.next; hit++; } } } } public boolean removeall(collection<? extends e> c) { int prevsize = this.size; (e item: c) { remove(item); } homecoming (prevsize < this.size); } public int size() { homecoming this.size; } public boolean isempty() { homecoming (this.head == null); } public void clear() { this.head = null; this.tail = null; size = 0; } private class node<e> { private e data; private node next; private node(e data, node next) { this.data = data; this.next = next; } } }

this interface class

/** * linkedlist interface defines set of standard operations * typically associated linked list, such adding, removing, , checking * see if list contains item. * * note while linked list implementation should create utilize of node * class, methods below take in , homecoming instances of generic type, * not nodes themselves. * * @author justin nieto * @version 1.1 * @param <e> type of elements store in linked list */ public interface linkedlist<e> { /** * returns element in linked list @ specified index. * not alter contents of list in way. if given * index negative or greater maximum possible index, returns * null. * * @param index of element retrieved * @return element @ given index or null if index out of bounds */ e get(int index); /** * adds specified piece of info end of linked list. * method should execute in o(1) (constant) time. means * should not iterate on whole list add together item end * (we check this). * * @param info object added linked list */ void add(e data); /** * adds given piece of info linked list @ given index. * items @ index or after index should * shifted downwards one. if index specified not valid, returns * false. otherwise, returns true. * * if index specified 0 or if 1 larger maximum * current index (ie if index equal size of linked list), * method should execute in o(1) (constant) time. means * should not iterate on entire list add together element, * unnecessary so. * * @param index index @ add together item * @param info item added te linked list * @return true if info added @ given index */ boolean add(int index, e data); /** * adds each element in collection end of linked list. * * @param c collection of items add together end of linked list */ void addall(collection<? extends e> c); /** * determines whether or not given piece of info in linked list. * * @param info item check * @return true if linked list contains item, false otherwise */ boolean contains(e data); /** * determines whether or not every element of given collection * in linked list. * * @param c collection of elements check * @return true if list contains every element in collection */ boolean containsall(collection<? extends e> c); /** * finds first element of list equal given piece of info * , removes list. returns false if given piece of info * not in list , hence cannot removed. * * @param info piece of info removed list * @return true if item removed, false if list not contain */ boolean remove(e data); /** * removes , returns item in list @ given index. * items @ indices after given index shifted downwards one. * * @param index index of item remove linked list * @return removed item */ e remove(int index); /** * removes each element in given collection linked list. * * @param c collection of items remove * @return true if each element in collection removed. */ boolean removeall(collection<? extends e> c); /** * returns number of elements in linked list. method * should execute in o(1) (constant) time. means should not * iterate on entire list count number of items, rather * should maintain size variable can homecoming here. * * @return number of elements in linked list */ int size(); /** * returns true if linked list has no elements. * * @return true if list empty, false otherwise */ boolean isempty(); /** * removes elements list. after calling method, * isempty method should homecoming true. */ void clear(); }

node inner class of singlylinkedlist, shares type parameter e. hence create node non-generic class.

private class node { private e data; private node next; private node(e data, node next) { this.data = data; this.next = next; } }

that removes many of problems, because before node generic class , using node without type parameter.

an alternative solution create node static nested class. means instance of node not belong instance of singlylinkedlist. because of this, static version of node need own type parameter.

private static class node<e> { private e data; private node<e> next; private node(e data, node<e> next) { this.data = data; this.next = next; } }

notice in version, node generic, had add together <e> after every time appeared. if go static approach, have add together <e> after every occurrence of node in rest of class. there lots of these.

finally, message "linkedlist not take parameters" self-explanatory. presumably says interface linkedlist when should interface linkedlist<e>.

java generics interface linked-list

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -