Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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#_Arrays - Fatal编程技术网

C# 在C中切片数组的正确形式#

C# 在C中切片数组的正确形式#,c#,arrays,C#,Arrays,我想问一下如何在不使用array.Copy的情况下对数组进行切片。 我会给你一个我想要达到的目标的例子,这样你就可以理解我了 假设我有这个数组。称为原创 [1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15] 我想从给定长度的起始索引中获取一个副本数组,假设我想要元素1到元素6,我想要代码执行如下任务 int startIndex = 0; int lenght= 5; int[] CopyArray = ArrayFromRa

我想问一下如何在不使用array.Copy的情况下对数组进行切片。 我会给你一个我想要达到的目标的例子,这样你就可以理解我了

假设我有这个数组。称为原创

[1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15]

我想从给定长度的起始索引中获取一个副本数组,假设我想要元素1到元素6,我想要代码执行如下任务

int startIndex = 0;
int lenght= 5;
int[] CopyArray = ArrayFromRange(Original, startIndex, length);
那么copyArray将是:

[1 | 2 | 3 | 4 | 5] 我不想使用Array.Copy,因为我将循环此操作以获取后续切片

所以我会这么做

int length = 3;
for(int i = 0; i < OriginalArray.Length; i++)
{
     int[] CopyArray = ArrayFromRange(OriginalArray, i, length);
     // perform some operations
}
int-length=3;
for(int i=0;i
这将在每次循环执行时为我提供一个包含4个元素的数组,然后我将执行一些操作。但是如果我做了
Array.Copy
它会抛出一个
OutOfBoundsException
当循环中的I得到值13时,它会尝试复制不存在的数组[15]。我想避免这些错误


我正在开发Winforms、.NET4.0,我认为最好的方法是使用IEnumerable对象,特别是使用LINQ。 IEnumerable是一个接口,意思是“您可以在此对象上调用foreach”。它是为数组实现的,也为用于查询集合的其他一些对象实现的——这一点的一部分是,您不需要特别了解这些对象是什么;只要在你需要的时候列举每一项就行了

using System.Linq; // put this at the top of the .cs, not mid-code.

int startIndex = 0;
int lenght= 5;
IEnumerable<int> CopyArray = Original.Skip(startIndex).Take(lenght);

您仍然可以使用
Array.Copy()
,但只需确保索引在范围内:

T[] ArrayFromRange<T>(T[] originalArray, int startIndex, int length)
{
  int actualLength = Math.Min(length, originalArray.Length - startIndex);
  T[] copy = new T[actualLength];
  Array.Copy(originalArray, startIndex, copy, 0, actualLength);
  return copy;
}
T[]数组fromrange(T[]原始数组,整数起始索引,整数长度)
{
int actualLength=Math.Min(长度、原始阵列长度-起始索引);
T[]复制=新的T[实际长度];
数组.副本(原始数组,起始索引,副本,0,实际长度);
返回副本;
}

在您的示例中,循环中的最后两个数组是
{14,15}
{15}

如果您喜欢返回缩短数组的方法,我可以提供其中一个,但我假设这是您想要的:(是的,这已经过测试)

使用系统;
名称空间数组片
{
班级计划
{
静态void Main(字符串[]参数)
{
字节[]数组1={5,10,15,20,25,30,35,40,45,50};
字节[]array2=GetSlice(array1,5,10);
for(int i=0;i
与论坛网站不同,我们不会在网站上使用“谢谢”或“感谢任何帮助”或签名。请参阅“.Check out ArraySlice,它为您提供了原始阵列的纯视图,而无需复制任何内容:只是一个注释,您可能需要抛出一个额外的
.ToArray()
.ToList())
否则,它的执行将被延迟,并在每次迭代时运行。这意味着如果原始数组中的值发生变化,那么
CopyArray
在迭代时也会产生变化的值。编辑:您可能还想提到
Skip
Take
足够宽松,它们可以不会抛出
IndexOutOfRangeException
错误。啊,是的,绝对是。这对所有用例来说都不是最优的,但听起来好像是针对手头的问题。另一方面,在一些有趣的情况下,每次都有一个自动进行筛选/排序/等的变量非常有用。@Chris Sinclair谢谢你哦,这就是我需要的。我对Linq几乎一无所知,但在阅读了Skip、Take和ToArray()的文档之后,我能够编写它,更重要的是,理解它。
T[] ArrayFromRange<T>(T[] originalArray, int startIndex, int length)
{
  int actualLength = Math.Min(length, originalArray.Length - startIndex);
  T[] copy = new T[actualLength];
  Array.Copy(originalArray, startIndex, copy, 0, actualLength);
  return copy;
}
using System;
namespace ArraySlice
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] array1 = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
            byte[] array2 = GetSlice<byte>(array1, 5, 10);
            for (int i = 0; i < array2.Length; i++)
            {
                Console.WriteLine(array2[i]);
            }
            Console.ReadKey();
        }
        static T[] GetSlice<T>(T[] array, int start, int length)
        {
            T[] result = new T[length];
            for (int i = 0; i < length; i++)
            {
                result[i] = ((start + i) < array.Length) ? array[start + i] : default(T);
            }
            return result;
        }
    }
}