簡體   English   中英

如何在java中使用通用類型的節點創建get方法

[英]How to create a get Method with nodes off a generic type in java

我正在實現一個循環的 DoublyLinkedList 數據結構。 與單向鏈表一樣,雙向鏈表中的節點引用下一個節點,但與單向鏈表不同的是,雙向鏈表中的節點也引用前一個節點。

另外,由於鏈表是“循環”的,鏈表最后一個節點中的“next”引用指向鏈表中的第一個節點,而鏈表中第一個節點中的“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)
{

}

另外,我的 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.
}

我遇到的另一個問題是,在我的節點類中,我可以在沒有它的情況下繼續前進。 讓我們更新它

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

您也可以使用哈希映射,並且您將獲得恆定時間的數據

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM