Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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中创建一个get方法,使节点脱离泛型类型_Java_Generics_Get_Nodes_Doubly Linked List - Fatal编程技术网

如何在java中创建一个get方法,使节点脱离泛型类型

如何在java中创建一个get方法,使节点脱离泛型类型,java,generics,get,nodes,doubly-linked-list,Java,Generics,Get,Nodes,Doubly Linked List,我正在实现一个循环双链接列表数据结构。与单链接列表一样,双链接列表中的节点具有对下一个节点的引用,但与单链接列表不同,双链接列表中的节点也具有对前一个节点的引用 此外,由于列表是“循环的”,因此列表中最后一个节点中的“下一个”引用指向列表中的第一个节点,而列表中第一个节点中的“上一个”引用指向列表中的最后一个节点 我需要帮助启动我的get方法,我一直在四处寻找,但我找不到任何可以帮助我的东西,因为我正在使用泛型类型。我需要返回E,并且每隔一个例子都会以int为例显示它。这是我的密码: publi

我正在实现一个循环双链接列表数据结构。与单链接列表一样,双链接列表中的节点具有对下一个节点的引用,但与单链接列表不同,双链接列表中的节点也具有对前一个节点的引用

此外,由于列表是“循环的”,因此列表中最后一个节点中的“下一个”引用指向列表中的第一个节点,而列表中第一个节点中的“上一个”引用指向列表中的最后一个节点

我需要帮助启动我的get方法,我一直在四处寻找,但我找不到任何可以帮助我的东西,因为我正在使用泛型类型。我需要返回E,并且每隔一个例子都会以int为例显示它。这是我的密码:

public class DoublyLinkedList<E>
{
private Node first;
private int size;

@SuppressWarnings("unchecked")
public void add(E value)
{
    if (first == null)
    {
        first = new Node(value, null, null);
        first.next = first;
        first.prev = first;
    }
    else
        {
        first.prev.next = new Node(value, first, first.prev);
        first.prev = first.prev.next;
    }
    size++;
}
private class Node<E>
{
    private E data;
    private Node next;
    private Node prev;

    public Node(E data, Node next, Node prev)
    {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
}
@SuppressWarnings("unchecked")
public void add(int index, E value)
{
    if (first.data == null)
    {
        throw new IndexOutOfBoundsException();
    } else if (index == 0)
    {
        first = new Node(value, first.next, first.prev);
    }
    else
        {
        Node current = first;
        for (int i = 0; i < index - 1; i++)
        {
            current = current.next;
        }
        current.next = new Node(value, current.next, current.prev);
    }
}
@SuppressWarnings("unchecked")
public void remove(int index)
{
    if (first.data == null)
    {
        throw new IndexOutOfBoundsException();
    }
    else if (index == 0)
    {
        first = first.next;
    }
    else
        {
            Node current = first.next;
            for (int i = 0; i < index - 1; i++)
        {
            current = current.next;
        }
        current.next = current.next.next;
    }
    size--;
}

另外,我的remove方法不准确,但我会自己解决这个问题,我只需要帮助我使用get方法

我明白了,我只是对这个问题没有得到任何回答感到震惊。不管是哪种方式,我都会写一些评论,这样它就可以指导未来的观众,让他们在这个问题上苦苦挣扎

@SuppressWarnings("unchecked")
public E get(int index)
{
    if(index < 0) //The index needs to be above 0.
    {
        throw new IndexOutOfBoundsException(); 
    }
    if(index > size) //Since we're going to run a for loop, we need to make sure the index doesn't go past the size of the list. 
    {
        throw new IndexOutOfBoundsException();
    }
    Node current = first; //We want to create another node to perform this method.
    for (int i = 0; i < index; i++) //We are going to set i to 0 and loop around this for loop until we reach the index. 
    {
        current = current.next;
    }
    return (E) current.data; //Since we are working with generics, we need to return a type E, and it needs to be in parenthesis so it gets that object.
}
@SuppressWarnings(“未选中”)
公共E-get(int索引)
{
if(index<0)//索引需要大于0。
{
抛出新的IndexOutOfBoundsException();
}
if(index>size)//因为我们要运行for循环,所以需要确保索引没有超过列表的大小。
{
抛出新的IndexOutOfBoundsException();
}
Node current=first;//我们想创建另一个节点来执行此方法。
for(inti=0;i
我遇到的另一个问题是,在我的节点类中,我有一个。让我们将其更新为

private class Node
{
    private E data;
    private Node next;
    private Node prev;

    public Node(E data, Node next, Node prev)
    {
        this.data = data;
        this.next = next;
        this.prev = prev;
    }
}
现在,我的getMethod()将如下所示:

@SuppressWarnings("unchecked")
public E get(int index)
{
    if(index < 0)
    {
        throw new IndexOutOfBoundsException();
    }
    if(index > size)
    {
        throw new IndexOutOfBoundsException();
    }
    Node current = first;
    for (int i = 0; i < index; i++)
    {
        current = current.next;
    }
    return current.data;
}
@SuppressWarnings(“未选中”)
公共E-get(int索引)
{
如果(指数<0)
{
抛出新的IndexOutOfBoundsException();
}
如果(索引>大小)
{
抛出新的IndexOutOfBoundsException();
}
节点电流=第一;
对于(int i=0;i
您还可以使用哈希映射,并且您将在固定时间内获得数据

public T get(int position){
    Node<T> node = map.get(position);
    T dat = node.getData();
    return dat;
}
public T get(int位置){
Node=map.get(位置);
T dat=node.getData();
返回数据;
}

get函数应该相当简单。由于类有一个size参数,如果index>=size,则它是一个无效的索引,因此代码不需要进行特殊检查来查看节点引用是否在列表中循环前进。如果index==0,则返回first.data。否则,请复制first,如“node=first”,然后将node=node.next“index”次数提前,并返回node.data。@rcgldr考虑到first.data不是类型E,它是一个对象,这将不起作用。我只是说,我不知道如何将其转换为E。@rcgldr private E data在一个私有类中,这使得它很难访问,但我的指示很清楚,它是这样设置的。根据我的规则,我必须保持node类的原样。@rcgldr我找到了它。嗨,Jorge,在我看来,使用backing
HashMap
来存储数据首先违背了使用
DoublyLinkedList
的目的。
public T get(int position){
    Node<T> node = map.get(position);
    T dat = node.getData();
    return dat;
}