Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的数组裁剪与合并#_C#_Arraylist - Fatal编程技术网

C# C语言中的数组裁剪与合并#

C# C语言中的数组裁剪与合并#,c#,arraylist,C#,Arraylist,我正在尝试优化一个用C#制作的蛇形游戏,其中我有一个ArrayList,其中包含存储的所有蛇身部分。我需要能够剪切这个ArrayList,就像它是一副牌一样,在这里,我将取牌组的顶部,并使其成为底部。这应该有助于澄清 01234 => 01 234 => 234 01 => 23401 此ArrayList最多可包含300个元素,我需要使此操作尽可能便宜,因为此游戏适用于移动设备。使用数组。复制应该是最快的方法。但它要求您使用T[]array,而不是ArrayList pub

我正在尝试优化一个用C#制作的蛇形游戏,其中我有一个ArrayList,其中包含存储的所有蛇身部分。我需要能够剪切这个ArrayList,就像它是一副牌一样,在这里,我将取牌组的顶部,并使其成为底部。这应该有助于澄清

01234 => 01 234 => 234 01 => 23401

此ArrayList最多可包含300个元素,我需要使此操作尽可能便宜,因为此游戏适用于移动设备。

使用
数组。复制
应该是最快的方法。但它要求您使用
T[]
array,而不是
ArrayList

public void CutTop<T>(this T[] source, int nbOfItemsToCut) {
    if(source == null)
        throw new ArgumentNullException("source");

    var length = source.Length;
    if(nbOfItemsToCut > length)
        throw new ArgumentException("nbOfItemsToCut");

    var temp = new T[nbOfItemsToCut];
    Array.Copy(source, temp, nbOfItemsToCut);

    Array.Copy(source, nbOfItemsToCut, source, 0, length - nbOfItemsToCut);
    Array.Copy(temp, 0, source, length - nbOfItemsToCut, nbOfItemsToCut);
}
public void CutTop(此T[]源,int nbOfItemsToCut){
if(source==null)
抛出新的ArgumentNullException(“源”);
var length=源.length;
如果(nbOfItemsToCut>长度)
抛出新的ArgumentException(“nbOfItemsToCut”);
var temp=新T[nbOfItemsToCut];
复制(源、临时、nbOfItemsToCut);
Copy(source,nbOfItemsToCut,source,0,length-nbOfItemsToCut);
复制(temp,0,source,length-nbOfItemsToCut,nbOfItemsToCut);
}

由于您希望将项目从头到尾移动,假设您不经常访问列表中的随机元素,则此处的
链接列表可能是一个不错的选择:

public void MoveToEnd<T>(LinkedList<T> list, int count)
{
    if(list == null)
        throw new ArgumentNullException("list");
    if(count < 0 || count > list.Count)
        throw new ArgumentOutOfRangeException("count");

    for (int i = 0; i < count; ++i)
    {
        list.AddLast(list.First.Value);
        list.RemoveFirst();
    }
}

var snake = new[] { 0, 1, 2, 3, 4 };
var list = new LinkedList<int>(snake);
Console.WriteLine(string.Join(" ", list)); // 0 1 2 3 4
MoveToEnd(list, 2);
Console.WriteLine(string.Join(" ", list)); // 2 3 4 0 1
public void MoveToEnd(链接列表,整数计数)
{
if(list==null)
抛出新的ArgumentNullException(“列表”);
如果(计数<0 | |计数>列表计数)
抛出新ArgumentOutOfRangeException(“计数”);
对于(int i=0;i

在任何情况下,我建议您测量在当前实现中执行此操作所需的时间,以确定是否确实有必要优化此部分。

一些过早优化的内容列表的类型是什么?您需要性能吗?不要使用
ArrayList
。使用通用集合代替。问题是我需要一个可调整大小的数组,在那里我可以放置身体部位。我正在使用Unity 3D,它是一个游戏对象阵列列表。我曾考虑过使用堆栈,但我需要能够一次访问所有元素,并且需要更改其大小。