Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何实现Java泛型_Java_Generics - Fatal编程技术网

如何实现Java泛型

如何实现Java泛型,java,generics,Java,Generics,我想我把我的类设置为正确的泛型,但当我尝试调用方法时,我似乎无法正确设置我的其他方法。我应该把我的变量转换成泛型的吗?还是将我的方法转换为变量 public class LinkedList<E> { // reference to the head node. private E head; private int listCount; public boolean delete(E string) // post: removes th

我想我把我的类设置为正确的泛型,但当我尝试调用方法时,我似乎无法正确设置我的其他方法。我应该把我的变量转换成泛型的吗?还是将我的方法转换为变量

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;
        }
    }


}
公共类链接列表
{
//对头部节点的引用。
私人E头;
私有整数列表计数;
公共布尔删除(E字符串)
//post:删除此列表中指定位置的元素。
{       
节点当前=head.getNext();
while(true){
如果(当前==null){
返回false;
}else if(current.getNext().getData().equals(字符串)){
if(current.getNext()==null){
current.setNext(空);
}否则{
current.setNext(current.getNext().getNext());
}
listCount--;//减少变量元素的数量
返回true;
}否则{
current=current.getNext();
}   
}
}
私有类节点
{
//引用链中的下一个节点,
E下一步;
//此节点携带的数据。
//可以是您需要的任何类型。
E数据;
//节点构造函数
公共节点(E_数据)
{
next=null;
数据=_数据;
}
//另一个节点构造函数,如果我们想
//指定要指向的节点。
公共节点(E_数据,E_下一个)
{
下一步=_下一步;
数据=_数据;
}
//这些方法应该是不言自明的
公共E getData()
{
返回数据;
}
公共无效设置数据(E_数据)
{
数据=_数据;
}
公共E getNext()
{
下一步返回;
}
公共无效设置下一步(E_下一步)
{
下一步=_下一步;
}
}
}

变量的类型有点混乱

  • 节点。下一步
    需要是一个
    节点
  • LinkedList.head
    需要是
    节点
  • 节点
    不需要是泛型的。(类型参数
    E
    在内部类的范围内。)
以下是编译的版本:

class LinkedList<E> {
    // reference to the head node.
    private Node head;
    private int listCount;

    public boolean delete(E string)
    // post: removes the element at the specified position in this list.
    {
        Node current = head;

        while (true) {
            if (current == null) {
                return false;
            } else if (current.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 {
        // reference to the next node in the chain,
        Node 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, Node _next) {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData() {
            return data;
        }

        public void setData(E _data) {
            data = _data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node _next) {
            next = _next;
        }
    }

}

到底是什么问题?你的
head
next
不应该是
Node
类型而不是
E
?你的代码看起来很好(尽管我上面的@JonK是绝对正确的),你能编辑你的问题以包括你想做什么的示例,但失败了吗?旁注:1:LinkedList已经被采用了,你真的应该换个名字。2:为节点类使用另一个参数名,例如T。3:我们通常期望一个名为AnythingList的类来实现接口列表。我真的希望
delete(E string)
中的变量命名是重构过程中一些奇怪的工件谢谢。我能把所有的东西都编译好。不过,我还有一个问题。我必须将类节点作为私有类节点我厌倦了将LInkedList作为公共类LInkedList,但这给我留下了来自eclipse的警告“LInkedList.Node是原始类型。对泛型类型LInkedList.Node的引用应该参数化”嗯。很有趣。我不能肯定这是因为什么。发布另一个问题,我们将看看其他人是否知道。。。
    Node current = head, prev = null;
    while (current != null) {
        if (current.getData().equals(string)) {
            // Remove current from list
            if (current == head) {
                head = current.getNext();
            } else {
                prev.setNext(current.getNext());
            }

            listCount--; // decrement the number of elements variable
            return true;
        }
        prev = current;
        current = current.getNext();
    }