C# 如何移动数组中的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/148786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:40:04  来源:igfitidea点击:

How to shift items in an array?

提问by Robert

I have an array of items that are time sensitive. After an amount of time, the last item needs to fall off and a new item is put at the beginning.

我有一系列对时间敏感的项目。一段时间后,最后一个项目需要脱落,并在开始时放置一个新项目。

What is the best way to do this?

做这个的最好方式是什么?

采纳答案by kafuchau

I would suggest using a queue, just a special instance of an array or list. When your timed event occurs, pop the last item from the queue, and then push your new item on.

我建议使用队列,只是数组或列表的一个特殊实例。当您的定时事件发生时,从队列中弹出最后一个项目,然后推送您的新项目。

回答by itsmatt

Use a Queueinstead.

改用队列

回答by James Curran

By using a Queue, preferably one implemented using a linked-list.

通过使用队列,最好是使用链表实现的队列。

回答by ConroyP

Have a look at using a Queuerather than a simple array.

看看使用队列而不是简单的数组。

回答by Unsliced

A queue would work if there a fixed number of items.

如果有固定数量的项目,队列将起作用。

Given that the 'amount of time' is known, how about a SortedDictionary with a DateTime key and override the Add method to remove all items with keys that are too old.

鉴于“时间量”是已知的,如何使用 DateTime 键的 SortedDictionary 并覆盖 Add 方法以删除所有键太旧的项目。

回答by Chris Marasti-Georg

LinkedList<T>has AddFirst and RemoveLast members that should work perfectly.

LinkedList<T>具有应该完美运行的 AddFirst 和 RemoveLast 成员。

EDIT: Looking at the Queue docs, it seems they use an internal array. As long as the implementation uses a circular-array type algorithm performance should be fine.

编辑:查看队列文档,它们似乎使用内部数组。只要实现使用循环数组类型的算法性能应该没问题。

回答by user7375

In csharp 3 you can do:

在 csharp 3 中,您可以执行以下操作:

original = new[] { newItem }.Concat(
    original.Take(original.Count() - 1)).ToArray()

But you are probably better off using a specialised datastructure

但是您可能最好使用专门的数据结构

回答by spoulson

Queueis great for FIFO arrays. For generic array handling, use List(T)'s Insert(0, x)and RemoveAt(0)methods to put or remove items in front of the list, for example.

Queue非常适合 FIFO 阵列。对于通用阵列处理,使用列表(T)Insert(0, x)RemoveAt(0)的方法在列表中的前放或删除项目,例如。

回答by Ken

Probably the easiest way to do this with an array is to use a circular index. Rather than always looking at array[n], you would reference array[cIndex] (where cIndex referrs to the item in the array being indexed (cIndex is incremented based on the arraySize (cIndex % arraySize)).

对数组执行此操作的最简单方法可能是使用循环索引。您不必总是查看 array[n],而是引用 array[cIndex](其中 cIndex 指的是被索引的数组中的项目(cIndex 根据 arraySize (cIndex % arraySize) 递增)。

When you choose to drop the oldest item in the array, you would simply reference the element located at ((cIndex + (arraySize - 1)) % arraySize).

当您选择删除数组中最旧的项目时,您只需引用位于 ((cIndex + (arraySize - 1)) % arraySize) 处的元素。

Alternatively, you could use a linkedList approach.

或者,您可以使用链表方法。

回答by Guillermo Phillips

Technically you need a deque. A queue has items pushed and popped off one end only. A deque is open at both ends.

从技术上讲,您需要一个双端队列。队列只有从一端推送和弹出的项目。双端队列在两端打开。

Most languages will allow array manipulation, just remove the first element and put another one on the end.

大多数语言都允许数组操作,只需删除第一个元素并在末尾放置另一个元素。

Alternatively you can shift every element, by looping. Just replace each element (starting from the oldest) with its neighbour. Then place the new item in the last element.

或者,您可以通过循环移动每个元素。只需用它的邻居替换每个元素(从最旧的开始)。然后将新项目放在最后一个元素中。

If you know that your deque won't go above a certain size, then you can make it circular. You'll need two pointers to tell you where the two ends are though. Adding and removing items, will increase/decrease your pointers accordingly. You'll have to detect a buffer overflow condition (i.e. your pointers 'cross'). And you'll have to use modular arithmetic so your pointers go in a circle around the array.

如果您知道您的双端队列不会超过特定大小,那么您可以将其设为圆形。您需要两个指针来告诉您两端的位置。添加和删​​除项目,将相应地增加/减少您的指针。您必须检测缓冲区溢出情况(即您的指针“交叉”)。而且您必须使用模算术,以便您的指针围绕数组走一圈。

Or you could time stamp each element in the array and remove them when they become too 'old'. You can either do this by keeping a separate array indexed in the same way, or by having an array of two element arrays, with the time stamp stored in one of the sub-elements.

或者您可以为数组中的每个元素添加时间戳,并在它们变得太“旧”时将其删除。您可以通过保持一个单独的数组以相同的方式索引来实现这一点,或者通过拥有一个包含两个元素数组的数组,并将时间戳存储在其中一个子元素中。