C#末由一个元件提高的阵列 [英] C# Increasing an array by one element at the end

查看:124
本文介绍了C#末由一个元件提高的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节目,我有一大堆的成长阵列,其中一个新元素是生长逐一数组的结束。我确定列出要在我的计划的一个关键部分的速度瓶颈,由于与阵列相比,它们的访问速度慢的时间 - 切换到阵列更高的性能极大到可接受的水平。因此,要发展我使用Array.Resize数组。这工作以及我实施限制数组大小约20个元素,所以Array.Resize的O(N)的性能是有界的。

In my program I have a bunch of growing arrays where a new element is grown one by one to the end of the array. I identified Lists to be a speed bottleneck in a critical part of my program due to their slow access time in comparison with an array - switching to an array increased performance tremendously to an acceptable level. So to grow the array i'm using Array.Resize. This works well as my implementation restricts the array size to approximately 20 elements, so the O(N) performance of Array.Resize is bounded.

但是,这将是更好的,如果有,只是增加在最后一个元素的数组,而不必使用Array.Resize的方法;我相信做旧阵列的复制到新大小的数组。

But it would be better if there was a way to just increase an array by one element at the end without having to use Array.Resize; which I believe does a copy of the old array to the newly sized array.

所以我的问题是,是否有添加一个元素到数组的末尾,而无需使用列表或Array.Resize更efficiant方法?

So my question is, is there a more efficiant method for adding one element to the end of an array without using List or Array.Resize?

推荐答案

A 列表的就像是一个数组常量时间访问。对于不断增长的阵列你确实应该使用列表

A List has constant time access just like an array. For 'growing arrays' you really should be using List.

当你知道你可以添加元素的数组,结构,你不想在每次添加一个新的大小。通常它是最好的,当它填补了一倍它的大小增长的数组。

When you know that you may be adding elements to an array backed structure, you don't want to add one new size at a time. Usually it is best to grow an array by doubling it's size when it fills up.

这篇关于C#末由一个元件提高的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆