代码之家  ›  专栏  ›  技术社区  ›  Adan Vivero

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

  •  0
  • Adan Vivero  · 技术社区  · 7 年前

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

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

    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)
    {
    
    }
    

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

    0 回复  |  直到 7 年前
        1
  •  1
  •   Adan Vivero    7 年前

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

    @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.
    }
    
        2
  •  0
  •   Adan Vivero    7 年前

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

    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;
    }
    
        3
  •  0
  •   Brydenr    6 年前

    public T get(int position){
        Node<T> node = map.get(position);
        T dat = node.getData();
        return dat;
    }