0

I have a byte array, for example:

{0,1,2,3,4,5,6,7,8,9}

I want to insert the smaller array into:

The array which I want to insert after the third index of the original array:

{11,11,11}

So the final array should look like this:

{0,1,2,3,11,11,11,4,5,6,7,8,9}

as you can see I need to keep the original values of the array, shift them left, and put the new array instead of it. I was looking into Array.Copy() But it's overwriting the original values, not shifting them

7
  • 1
    You'll need a new array to accommodate the combined one, so allocate that and use three times Array.Copy() to copy first part of array1, then array2, then second part of array1. Commented Oct 11, 2021 at 9:27
  • 1
    Is an array the best data structure to be using here? Even if it uses an array behind the scenes, surely just being able to write InsertRange(3, otherData) on a List<byte> would let you concentrate on getting work done rather than fussing around with copying array values around, etc. Commented Oct 11, 2021 at 9:35
  • You don't really want an array but a List<T>. Commented Oct 11, 2021 at 9:40
  • @Damien_The_Unbeliever i will need to write the result data with filestream into the file, that's why its byte array Commented Oct 11, 2021 at 9:41
  • 3
    If you are using .net 5.0 or later you can write a List<byte> to a FileStream without having to convert to an array using fileStream.Write(CollectionsMarshal.AsSpan(listOfBytes)); (This does not make a copy of the data so it's very efficient, but you mustn't change the listOfBytes in another thread while fileStream.Write() is doing its thing.) Commented Oct 11, 2021 at 9:51

2 Answers 2

2

You can try using Linq to query array and assign the result back to it:

 using System.Linq;

 ...

 int[] array = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 int[] insert = new int[] {11, 11, 11};

 array = array
   .Take(4)
   .Concat(insert)
   .Concat(array.Skip(4)) 
   .ToArray();  

If you are looking for low level Array methods:

int[] array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] insert = new int[] { 11, 11, 11 };

int at = 4;

Array.Resize<int>(ref array, array.Length + insert.Length);

Array.Copy(array, at, array, at + insert.Length, array.Length - at - insert.Length);
Array.Copy(insert, 0, array, at, insert.Length);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice minimal solution!
0

You can convert your array to an list. A list is like an array, but you can easily add, insert or remove values. See this code:

int[] array = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] insert = new int[] {11, 11, 11};

List<int> list = new List<int>(array);
list.InsertRange(4, insert);
        
array = list.ToArray(); // convert the list back to an array

Online demo: https://dotnetfiddle.net/GeEAeU

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.