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

查看:88
本文介绍了如何在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.

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

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方法的帮助,我一直在寻找,找不到任何可以帮助我的东西因为我正在使用泛型类型。我需要返回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)
{

}

此外,我的删除方法不准确,但是我我会发现自己一个人,我只需要我的get方法的帮助。

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

推荐答案

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

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天全站免登陆