我意识到我无法通过替换价值LinkedList.Enumerator.
例如,我尝试将以下Java代码移植到C#:
Java代码:
ListIterator<Double> itr1 = linkedList1.listIterator();
ListIterator<Double> itr2 = linkedList2.listIterator();
while(itr1.hasNext() && itr2.hasNext()){
Double d = itr1.next() + itr2.next();
itr1.set(d);
}
Run Code Online (Sandbox Code Playgroud)
C#代码:
LinkedList<Double>.Enumerator itr1 = linkedList1.GetEnumerator();
LinkedList<Double>.Enumerator itr2 = linkedList2.GetEnumerator();
while(itr1.MoveNext() && itr2.MoveNext()){
Double d = itr1.Current + itr2.Current;
// Opps. Compilation error!
itr1.Current = d;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用的任何其他技术?
C#的LinkedList枚举器枚举值,而不是节点.
如果你想像在Java版本中那样修改节点,我认为你必须手动"枚举"节点:
LinkedListNode<Double> nod1 = linkedList1.First;
LinkedListNode<Double> nod2 = linkedList2.First;
while (nod1 != null && nod2 != null)
{
Double d = nod1.Value + nod2.Value;
nod1.Value = d;
nod1 = nod1.Next;
nod2 = nod2.Next;
}
Run Code Online (Sandbox Code Playgroud)