2014-12-24 20 views
0

我想將C#代碼轉換爲Java。除了if條件下的三條線之外,我幾乎已經轉換了所有東西。Java中的LinkedListNode

的C#代碼

LinkedList<T> buk = new LinkedList(); 
LinkedListNode<T> current = buk.First; 
LinkedListNode<T> previous = null; 
if (fooCondition) { 
    previous = current.Previous; 
} else { 
    previous = current; 
    current = current.Next; 
} 

等效的Java代碼

LinkedList<T> buk = new LinkedList<>(); 
T current = buckets.getFirst(); 
T previous = null; 
if (fooCondition) { 
    ?     //previous = current.Previous; 
} else { 
    ?     //previous = current; 
    ?     //current = current.Next; 
} 

由於沒有LinkedListNode類在Java中,任何人可以提出什麼是Java中的等效代碼?

編輯

這似乎是完整的代碼,以獲得幫助是很重要的。下面是從link

protected void MergeBuckets() 
    { 
     LinkedListNode<Bucket> current = buckets.First; 
     LinkedListNode<Bucket> previous = null; 

     int k = (int)Math.Ceiling(1/epsilon);    // k=1/eps as integer 
     int kDiv2Add2 = (int)(Math.Ceiling(0.5 * k) + 2); // k/2 as integer 
     // at this point 1/k <= eps, k >= 2, hence requires eps >= 0.5 

     // number of concecutive buckets with same count causing a 
     // merge of the oldest two of those buckets 
     int numberOfSameCount = 0; 

     // traverse buckets from first to last, hence in order of 
     // descending timestamp and ascending count 
     while (current != null) 
     { 
      // previous and current bucket have same count, increment counter 
      if (previous != null && previous.Value.Count == current.Value.Count) 
       numberOfSameCount++; 
      // current is first with that count, reset counter to 1 
      else 
       numberOfSameCount = 1; 

      // detect need for a merge 
      if (numberOfSameCount == kDiv2Add2) 
      { 
       // merge buckets into current and remove previous 
       current.Value.Timestamp = previous.Value.Timestamp;     // take most recent timestamp 
       current.Value.Count = previous.Value.Count + current.Value.Count; // sum the counts of the buckets, 
                        // i.e. next power of two 

       buckets.Remove(previous); 

       // note that a merged bucket might cause a cascade of merges due to its new count, 
       // hence the new current node should point to the merged bucket otherwise the 
       // cascade might go unnoticed, temporarily violating the invariant! 

       previous = current.Previous; // merged bucket's previous, since old previous is removed 
       //current = current;   // trivial, merged bucket is new current 

       // at this iteration, the traversal stays in place 
      } 
      // no merge required, continue normally 
      else 
      { 
       previous = current;   // old current bucket or merged bucket 
       current = current.Next;  // current's or merged's next 

       // at this iteration, the traversal moves to the next (older) bucket 
      } 
     } 
    } 
+5

你爲什麼要做if/e以'真'作爲條件嗎? –

+0

我只是簡化它,有一個變量,如果條件檢查。 – user3212493

+1

簡化是好的,只要它保留什麼必要 –

回答

0

C#的功能,你不能讓一個LinkedList沒有爲它的節點。不幸的是,你的代碼沒有意義。

LinkedList節點(在本例中爲雙向鏈接)由下一個節點,前一個節點和節點中的數據以及訪問方法組成。它的實現非常簡單,因爲它只是一個數據存儲結構。

class LinkedListNode<T> { 
    LinkedListNode prevNode, nextNode; 
    T data; 

    public LinkedListNode getNext() { 
     return nextNode; 
    } 
    public LinkedListNode getPrev() { 
     return prevNode; 
    } 
    public T getValue() { 
     return data; 
    } 
    public void setNext(LinkedListNode n) { 
     nextNode = n; 
    } 
    public void setPrev(LinkedListNode n) { 
     prevNode = n; 
    } 
    public void setValue(T data) { 
     data = n; 
    } 
} 
+2

您還需要實現使用此LinkedListNode的鏈接列表。 – Sid

+2

其實在Java中,通常的方法是創建一個沒有節點的LinkedList。類LinkedList將在內部處理節點。 –

+1

@Christian Hujer在這種情況下,你已經聲明我們可以用迭代器遍歷鏈表。我們可以使用hasnext()方法來遍歷next.Can,我們可以遍歷嗎? – Razib

0

您可以編寫自己的LinkedListNode類的版本,其中包含previousnext,因爲它的屬性/字段。例如 -

class LinkedListNode{ 

LinkedListNode previous; 
LinkedListNode next; 

} 

然後添加一些getter和setter方法來訪問屬性/字段。您可以添加另一個屬性/字段來存儲節點的值。這是基本結構。這可能對你有幫助。你也可以看看this的鏈接。
謝謝

+0

LinkedListNode如何作爲LinkedList.getFirst()不返回LinkedListNode對象 – user3212493

1

Java類java.util.LinkedList有一個內部類LinkedList.Node這是private。不能直接訪問LinkedList中的Node。相反,請參考List.indexOf(E)List.add(E, int)ListIterator等方法,以便將元素插入特定位置。

final LinkedList<T> list = new LinkedList<>(); 
list.add(object1); 
if (cond) { 
    list.add(object2, list.indexOf(object1)); 
} else { 
    list.addFirst(object2); 
} 

一個經常用於處理在Java中LinkedList成語是創建LinkedList但主要使用ListIterator操作就可以了。

final LinkedList<T> list = new LinkedList<>(); 
final ListIterator<T> iterator = list.listIterator(); 
if (!cond && list.hasNext()) { 
    list.next(); 
} 
list.add(object2); 
4

你不能使用由LinkedList的,並使用其提供的方法提供的ListIterator瀏覽鏈表

ListIterator<T> listIterator = linkedListNode.listIterator(0); 
if(yourCondition && listIterator.hasNext()){ 
    T next = listIterator.next(); 
} 
else if (listIterator.hasPrevious()){ 
    T previous = listIterator.previous(); 
} 

希望它可以幫助

+0

如果直接應用到OP的問題,這有可能是最好的答案恕我直言,恕我直言。 –

+0

我同意,給他一個投票來呼籲關注它。但是,我想指出previous()將指針向後移動,而previousIndex()將僅提供對列表中前一個節點位置的引用。 – ThisClark

+0

我還要補充一點,你應該檢查這些操作是否可以通過使用hasPrevious()和hasNext()返回布爾值來指示是否可以執行。 –

-2
LinkedList<T> buk=new LinkedList<T>(); 
    //make list 
    T current=buk.getFirst(); 
    T previous=null; 

    if (fooCondition) { 
     previous = current.previous; 
    } else { 
     previous = current; 
     current = current.next; 
    } 

和T的結構:

Class T{ 
    public T previous; 
    public T next; 
    //rest 
} 
+0

這段代碼缺乏解釋,它假設'T'有'getPrevious()'和'getNext()'方法 - 它們與LinkedList有什麼關係?他們應該如何工作? –