如何在java中创建一个带有泛型类型的节点的get方法

问题描述 投票:0回答:2

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

此外,因为列表是“循环”的,所以列表中最后一个节点中的“下一个”引用指向列表中的第一个节点,列表中第一个节点中的“prev”引用指向最后一个节点。列表。

我需要帮助启动我的get方法,我一直在环顾四周,因为我正在使用Generic Type,所以找不到任何可以帮助我的东西。我需要返回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--;
}

我想不出一种方法可以开始这个,但基本上这个方法应该做的是返回列表中指定索引处的元素。如果index参数无效,则应抛出IndexOutOfBoundsException。

public E get(int index)
{

}

另外,我的删除方法不准确,但我会自己想出一个,我只需要帮助我的get方法。

java generics get nodes doubly-linked-list
2个回答
0
投票

我想通了,我很震惊,我没有得到任何回答这个问题。无论哪种方式,我都会写一些评论,以便引导未来的观众,他们正在努力解决这个问题。

@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.
}

0
投票

我遇到的另一个问题是,在我的Node类中,我有那个,当我没有它时就可以继续前进。让我们更新它

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.