我在内存中有一个字节数组,从文件中读取。我想在某个点(索引)分割字节数组,而不必只创建一个新的字节数组并一次复制每个字节,从而增加操作的内存占用。我想要的是这样的:

byte[] largeBytes = [1,2,3,4,5,6,7,8,9];  
byte[] smallPortion;  
smallPortion = split(largeBytes, 3);  

smallPortion 等于 1,2,3,4
largeBytes 等于 5,6,7,8,9

有帮助吗?

解决方案

我就是这样做的:

using System;
using System.Collections;
using System.Collections.Generic;

class ArrayView<T> : IEnumerable<T>
{
    private readonly T[] array;
    private readonly int offset, count;

    public ArrayView(T[] array, int offset, int count)
    {
        this.array = array;
        this.offset = offset;
        this.count = count;
    }

    public int Length
    {
        get { return count; }
    }

    public T this[int index]
    {
        get
        {
            if (index < 0 || index >= this.count)
                throw new IndexOutOfRangeException();
            else
                return this.array[offset + index];
        }
        set
        {
            if (index < 0 || index >= this.count)
                throw new IndexOutOfRangeException();
            else
                this.array[offset + index] = value;
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        for (int i = offset; i < offset + count; i++)
            yield return array[i];
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        IEnumerator<T> enumerator = this.GetEnumerator();
        while (enumerator.MoveNext())
        {
            yield return enumerator.Current;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        byte[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        ArrayView<byte> p1 = new ArrayView<byte>(arr, 0, 5);
        ArrayView<byte> p2 = new ArrayView<byte>(arr, 5, 5);
        Console.WriteLine("First array:");
        foreach (byte b in p1)
        {
            Console.Write(b);
        }
        Console.Write("\n");
        Console.WriteLine("Second array:");
        foreach (byte b in p2)
        {
            Console.Write(b);
        }
        Console.ReadKey();
    }
}

其他提示

供参考。 System.ArraySegment<T> 结构基本上是一样的 ArrayView<T> 在上面的代码中。如果您愿意,可以以相同的方式使用这种开箱即用的结构。

在带有 Linq 的 C# 中,您可以执行以下操作:

smallPortion = largeBytes.Take(4).ToArray();
largeBytes = largeBytes.Skip(4).Take(5).ToArray();

;)

试试这个:

private IEnumerable<byte[]> ArraySplit(byte[] bArray, int intBufforLengt)
    {
        int bArrayLenght = bArray.Length;
        byte[] bReturn = null;

        int i = 0;
        for (; bArrayLenght > (i + 1) * intBufforLengt; i++)
        {
            bReturn = new byte[intBufforLengt];
            Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLengt);
            yield return bReturn;
        }

        int intBufforLeft = bArrayLenght - i * intBufforLengt;
        if (intBufforLeft > 0)
        {
            bReturn = new byte[intBufforLeft];
            Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLeft);
            yield return bReturn;
        }
    }

我不确定你的意思是:

我想在某个点(索引)分割字节数组,而不必只创建一个新的字节数组并一次复制每个字节,从而增加操作的内存占用。

在大多数语言中,尤其是 C#,一旦分配了数组,就无法更改它的大小。听起来您正在寻找一种更改数组长度的方法,但您不能。您还希望以某种方式回收数组第二部分的内存,以创建第二个数组,但您也无法做到这一点。

总之:只需创建一个新数组。

作为 艾伦说, , 您可以使用 ArraySegment<T>. 。下面是一个扩展方法和使用示例:

public static class ArrayExtensionMethods
{
    public static ArraySegment<T> GetSegment<T>(this T[] arr, int offset, int? count = null)
    {
        if (count == null) { count = arr.Length - offset; }
        return new ArraySegment<T>(arr, offset, count.Value);
    }
}

void Main()
{
    byte[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    var p1 = arr.GetSegment(0, 5);
    var p2 = arr.GetSegment(5);
    Console.WriteLine("First array:");
    foreach (byte b in p1)
    {
        Console.Write(b);
    }
    Console.Write("\n");
    Console.WriteLine("Second array:");
    foreach (byte b in p2)
    {
        Console.Write(b);
    }
}

你不能。您可能想要的是保留起始点和项目数量;本质上,构建迭代器。如果这是C++,你可以使用 std::vector<int> 并使用内置的。

在 C# 中,我将构建一个小型迭代器类,用于保存起始索引、计数和实现 IEnumerable<>.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top