5

This might be a silly question, but have not found a simple answer yet...

I'm trying to insert a simple c# byte array into another byte array at a specific position. E.g. the existing bytes should be not be overridden, but just moved further back. Really just like you copy page some text block inside an existing text block.

  1. So far, I would create a new array with the length of both existing arrays.
  2. Copy the first array into the new one until the position where the insert starts.
  3. Add the inserted array
  4. Add the rest of the existing array.

But I would assume this is something common and should be easier? Or am I wrong?

1
  • Are you sure that you don't want to use a List instead? Commented Jan 6, 2011 at 15:39

6 Answers 6

8

Use a List<byte> instead of a byte[]; it will provide the flexibility you are looking for...

List<byte> b1 = new List<byte>() { 45, 46, 47, 50, 51, 52 };
List<byte> b2 = new List<byte> { 48, 49 };
b1.InsertRange(3, b2);

Then if you need to go back to a byte[] for whatever reason you can call...

b1.ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

I should have mentioned, that I'm doing this with files who could get pretty big. But I guess I got the answers I was looking for. Multiple ways would work, but as in these reality TV shows, I had to choose one :-)
@Remy: Note that Insert is an O(n) operation. If you expect to be using many Inserts of very small lists of bytes, you should consider using a LinkedList<T>.
7

But I would assume this is something common

If inserting a large chunk of data into the middle of another large chunk of data is something you do often then you might consider using a data structure designed to do that. An array is designed to be fixed in size and mutable in content. If your requirement includes "variable in size" then an array is the wrong data type for you. Consider instead a doubly linked list or a catenable deque.

and should be easier?

You've identified a trivial four-step algorithm that does what you want. It doesn't get much easier than that.

Comments

5

Look at Array.CopyTo.

Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index.

3 Comments

Right, that would help to implement the flow what I described above. Was hoping that there is a simpler way?
@Remy - Not really, not if you want to use arrays.
-1 CopyTo is going to override the contents based on the starting index; which the OP stated they did not want to do.
1

If performance is not important, consider:

var combined = first.Take(insertPosition)
                    .Concat(second)
                    .Concat(first.Skip(insertPosition))
                    .ToArray();

I suppose this is pretty much the four-step algo you've suggested, except the first step "comes at the end." However, do note that this is inefficient for a number of reasons, including a needlessly dynamic buffer and a redundant partial enumeration of the first array.

Otherwise, what you have suggested is perfectly fine.

Comments

0

If you could trade up your object for something a bit bigger you could look at things like a List which has an InsertRange method that does what you want (http://msdn.microsoft.com/en-us/library/884ee1fz.aspx). Of course using a different object may not be an option but its a suggestion for an easy way to do things. Also other objects might be more useful. Shop around... :)

Comments

0

An array, by definition, has a fixed size. You cannot insert or remove elements, just overwrite elements.

You should a list instead. Lists provide methods to insert or remove elements and subranges.

2 Comments

Of course, as an implementation detail, a list is just a view of an array; all of the insertions and removals just do the array copying operations for you.
Yes, indeed. At the end, class List is doing the same algorithm internally which the OP didn't wanted to write on its own.

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.