如何在java中使用通用类型的节点创建get方法 [英] How to create a get Method with nodes off a generic type in java

查看:34
本文介绍了如何在java中使用通用类型的节点创建get方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个循环的 DoublyLinkedList 数据结构.与单向链表一样,双向链表中的节点都引用了下一个节点,但与单向链表不同的是,双向链表中的节点也引用了前一个节点.

I am implementing a cyclic DoublyLinkedList data structure. Like a singly linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node.

另外,由于链表是循环"的,链表最后一个节点的next"引用指向链表的第一个节点,而链表第一个节点的prev"引用指向的是列表中的最后一个节点.

Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.

我需要帮助来启动我的 get 方法,我一直在环顾四周,但我找不到任何可以帮助我的东西,因为我正在使用 Generic Type .我需要返回 E 并且所有其他示例都以 int 为例向我展示它.这是我的代码:

I need help starting my get method, I've been looking around and I couldn't find anything that could help me since I am working with a Generic Type . I need to return E and every other examples show me it with int as an example. Here is my code:

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.

I can't think of a way to get started on this, but basically what this method should do is return the element at the specified index in the list. If the index parameter is invalid, an IndexOutOfBoundsException should be thrown.

public E get(int index)
{

}

另外,我的 remove 方法不准确,但我会自己弄清楚,我只是需要有关 get 方法的帮助.

Also, my remove method isn't accurate, but I'll figure that one out myself, I just need help with my get method.

推荐答案

我遇到的另一个问题是,在我的节点类中,我可以在没有它的情况下继续前进.让我们将其更新为

Another problem I sort of had was that in my Node Class I had the in there, when I could've moved on without it. Lets update it to be

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() 将如下所示:

And now my getMethod() will be as follows:

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

这篇关于如何在java中使用通用类型的节点创建get方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆