观察到的堆栈和队列 [英] Observable Stack and Queue
问题描述
我在寻找一个 INotifyCollectionChanged
实施堆栈
和队列
。我可以推出自己的,但我不希望推倒重来。
I'm looking for an INotifyCollectionChanged
implementation of Stack
and Queue
. I could roll my own but I don't want to reinvent the wheel.
推荐答案
使用栈和队列(按照定义),你只能访问队列的堆栈或头顶。这就是从列表
区分它们。 (因此,这就是为什么你还没有找到一个)
With Stacks and Queues (almost by definition) you only have access to the top of the stack or head of the queue. It's what differentiates them from a List
. (and so, that's why you haven't found one)
要回答虽然你可以写你自己的,我会做从的ObservableCollection
导出,然后在实施推栈的情况下,
为插入
偏移0(和流行为返回指数0,那么 RemoveAt移除
指数0 );或用队列你可以只
到列表的末尾添加到排队
,以及抢并删除第项目,与堆栈,用于出列
。在插入
,添加
和 RemoveAt移除
的操作将被称为上底层的ObservableCollection
等导致 CollectionChanged
事件被解雇。
To answer though you could write your own, I would do it by deriving from ObservableCollection
, then in the case of a stack implementing the Push
as an Insert
at offset 0 (and pop as returning index 0 then RemoveAt
index 0); or with a queue you could just Add
to the end of the list to Enqueue
, and the grab and remove the first item, as with the stack, for Dequeue
. The Insert
, Add
and RemoveAt
operations would be called on the underlying ObservableCollection
and so cause the CollectionChanged
event to be fired.
您也可能会说,你只是想绑定或当一个项目,你都应该有机会获得变化的通知。你会再次创建自己的类,从堆栈或队列导出,手动触发CollectionChanged事件时:
You might also be saying that you simply want to bind or be notified when the one item you are supposed to have access to changes. You would create your own class again, derived from Stack or Queue, and fire the CollectionChanged event manually when:
- 的东西被压入或弹出从堆栈中
- 东西是从队列中离开
- 东西被排队队列,当队列为previously空
这篇关于观察到的堆栈和队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!