在 C# 中按最后一个而不是第一个调整数组大小

发布于 2024-10-09 19:17:43 字数 697 浏览 2 评论 0 原文

我有一个 Class 元素数组,通过 int 变量,我需要将该数组的大小调整为最后 X 个元素。

例如,我有一个数组:

Array[0] = Msg1
Array[1] = Msg2
Array[2] = Msg3
Array[3] = Msg4
Array[4] = Msg5
Array[5] = Msg6
Array[6] = Msg7
Array[7] = Msg8
Array[8] = Msg9
Array[9] = Msg10

并且我只需要数组中的最后 8 个元素。

我无法使用 Array.Resize 函数,因为结果将是:

Array[0] = Msg1
Array[1] = Msg2
Array[2] = Msg3
Array[3] = Msg4
Array[4] = Msg5
Array[5] = Msg6
Array[6] = Msg7
Array[7] = Msg8

我需要这样的东西:

Array[0] = Msg3
Array[1] = Msg4
Array[2] = Msg5
Array[3] = Msg6
Array[4] = Msg7
Array[5] = Msg8
Array[6] = Msg9
Array[7] = Msg10

我怎样才能做到这一点?我希望我的问题很清楚。

谢谢。

I have an Array of Class elements, and by an int variable i need to resize this array to the last X elements.

So for example i have an array with:

Array[0] = Msg1
Array[1] = Msg2
Array[2] = Msg3
Array[3] = Msg4
Array[4] = Msg5
Array[5] = Msg6
Array[6] = Msg7
Array[7] = Msg8
Array[8] = Msg9
Array[9] = Msg10

and i need to have only the last 8 elements in the array.

i cannot use the Array.Resize function because the result would be:

Array[0] = Msg1
Array[1] = Msg2
Array[2] = Msg3
Array[3] = Msg4
Array[4] = Msg5
Array[5] = Msg6
Array[6] = Msg7
Array[7] = Msg8

and i need something like this:

Array[0] = Msg3
Array[1] = Msg4
Array[2] = Msg5
Array[3] = Msg6
Array[4] = Msg7
Array[5] = Msg8
Array[6] = Msg9
Array[7] = Msg10

How can i do this? i hope my problem is clear.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素手挽清风 2024-10-16 19:17:43

使用 LINQ:

array = array.Skip(array.Length - 8).ToArray();

不使用 LINQ:

int[] temp = new int[8];
Array.Copy(array, array.Length - temp.Length, temp, 0, temp.Length);
array = temp;

With LINQ:

array = array.Skip(array.Length - 8).ToArray();

Wthout LINQ:

int[] temp = new int[8];
Array.Copy(array, array.Length - temp.Length, temp, 0, temp.Length);
array = temp;
蓦然回首 2024-10-16 19:17:43

创建一个新数组,然后执行
Array.Copy()
或者,使用 LINQ,您可以做到
array.Skip(array.Length - 8).Take(8).ToArray()

Create a new Array, then do
Array.Copy().
Or, with LINQ, you can do
array.Skip(array.Length - 8).Take(8).ToArray()

口干舌燥 2024-10-16 19:17:43

它们的顺序必须相同吗?

像哎呀这样的东西怎么样

Reverse(Array);
Resize(ref Array, 8)?

,你可以再次逆转以取回你的订单

Do they have to be in the same order?

How about something like

Reverse(Array);
Resize(ref Array, 8)?

Heck, you could reverse again to get your order back

月亮是我掰弯的 2024-10-16 19:17:43

我认为您可能使用了错误的集合。从您的示例来看,它看起来像一个 队列LinkedList 会更好选择,这两者都会使您的操作变得更加容易,甚至微不足道,并且自 2.0 以来两者都已成为 .NET FCL 的一部分。 (不需要专门的集合库。)

对于队列集合,您可以简单地调用 出队 n次,并且使用 LinkedList 您可以调用 RemoveFirst n 次为您提供最终结果。

除了修剪集合的开头之外,您还将使用其他 LinkedList 或 Queue 操作,这可能也很有用。

I think that you're probably using the wrong collection. From your example it looks like a Queue or a LinkedList would be better choices, both of which would make your operation much easier, even trivial AND both have been part of the .NET FCL since 2.0. (No need for a specialized collections library.)

For a Queue collection you could simply call Dequeue n-times and with a LinkedList you could call RemoveFirst n-times giving you your final result.

And besides trimming the beginning of the collection you will be using other LinkedList or Queue operations which will probably be useful as well.

山田美奈子 2024-10-16 19:17:43

许多采用数组作为参数的函数都有重载,这些重载采用附加参数,例如 startIndex 以及 length/count结束索引。第二个参数有时是可选的,因为可以使用 Array.Length 属性推断出长度或 endIndex。

例如:

SomeFunction<T>(T[] array, int startIndex, int length)

或者

SomeFunction<T>(T[] array, int startIndex, int endIndex)

只是把这个扔出去;也许这是您的一个选择。

Many functions that take arrays as parameters have overloads that take additional parameters such as a startIndex along with either a length/count or an endIndex. The second parameter is sometimes optional, since the length or endIndex can be inferred using the Array.Length property.

For example:

SomeFunction<T>(T[] array, int startIndex, int length)

or

SomeFunction<T>(T[] array, int startIndex, int endIndex)

Just throwing this out there; perhaps this is an option for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文