Scala DoubleLinkedList 中的当前元素是什么? [英] What is the current element in a Scala DoubleLinkedList?
问题描述
我正在查看使用一个DoubleLinkedList.它的 remove() 方法说从双链表中删除当前节点." 但是页面中没有其他对 current 的引用.
I'm looking at using a DoubleLinkedList. It's remove() method says "Removes the current node from the double linked list." but there are no other references to current in the page.
当前节点是什么,我如何设置它,当然这不是删除项目的唯一方法?
What is the current node, how do I set it, and surely this can't be the only way of removing an item?
推荐答案
DoubleLinkedList
同时是列表本身和一个列表节点,类似于::
用于常规 List
.您可以分别使用 next
和 prev
从一个单元格导航到下一个或上一个单元格,并使用 elem
获取单元格的值>.
A DoubleLinkedList
is at the same time the list itself and a list node, similar to the ::
for a regular List
. You can navigate from one cell to the next or to the previous one with next
and prev
, respectively, and get the value of a cell with elem
.
scala> val list = collection.mutable.DoubleLinkedList(1,2,3,4,5)
list: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5)
scala> list.next.next.remove() // list.next.next points on 3rd cell
scala> list
res0: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 4, 5)
删除第一个单元格时要小心,因为您需要将包含列表的 var 重新分配给下一个单元格:
Be careful if you remove the first cell, as you’ll need to reassign your var holding the list to the next cell:
scala> val list = collection.mutable.DoubleLinkedList(1,2,3,4,5)
list: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5)
scala> list.remove() // remove first item
scala> list // this is now a 'dangling' cell, although it still points to the rest of the list
res6: scala.collection.mutable.DoubleLinkedList[Int] = DoubleLinkedList(1, 2, 3, 4, 5) // uh? didn't I remove the first cell?
scala> list.next.prev // we can check that it is not pointed back to by its next cell
res7: scala.collection.mutable.DoubleLinkedList[Int] = null
这篇关于Scala DoubleLinkedList 中的当前元素是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!