Я думаю, что я настроил свой класс правильно, чтобы быть общим, но когда я пытаюсь вызвать методы, которые, похоже, не корректно настраивают мои другие методы. Я предположил, что мои переменные являются общими? или я передаю свои методы переменным?Как реализовать Java Generics
public class LinkedList<E>
{
// reference to the head node.
private E head;
private int listCount;
public boolean delete(E string)
// post: removes the element at the specified position in this list.
{
Node current = head.getNext();
while(true){
if(current == null){
return false;
}else if(current.getNext().getData().equals(string)){
if(current.getNext() == null){
current.setNext(null);
}else{
current.setNext(current.getNext().getNext());
}
listCount--; // decrement the number of elements variable
return true;
}else{
current = current.getNext();
}
}
}
private class Node<E extends Comparable<E>>
{
// reference to the next node in the chain,
E next;
// data carried by this node.
// could be of any type you need.
E data;
// Node constructor
public Node(E _data)
{
next = null;
data = _data;
}
// another Node constructor if we want to
// specify the node to point to.
public Node(E _data, E _next)
{
next = _next;
data = _data;
}
// these methods should be self-explanatory
public E getData()
{
return data;
}
public void setData(E _data)
{
data = _data;
}
public E getNext()
{
return next;
}
public void setNext(E _next)
{
next = _next;
}
}
}
Что именно проблема? – aioobe
Не должно 'head' и' next' быть типа 'Node' вместо 'E'? –
JonK
Ваш код выглядит нормально (хотя @JonK выше меня абсолютно прав), не могли бы вы изменить свой вопрос, чтобы включить пример того, что вы хотите сделать, но не удается? – MrHug