5

I was looking for a way, how to set specific values for specific range in an array.

Something like this

Pseudocode:

var s = new uint[64];
s[ 0..15] := { 2, 4, 6, 3,  1, 7, 8, 9,  7, 11, 37, 32,  19, 16, 178, 2200 }
s[16..31] := ... 

I was trying to find something like this in C#, but with no luck. I am trying to come with something like this:

public void SetArrayValues(int startIndex, uint[] values) 
{
    var length = values.Length;
    this.array[startIndex, startIndex + length] = values;
}

The only thing I was able to find was System.Array.SetValue but this does not meet my requirements.

jordanz
  • 367
  • 4
  • 12
Kajiyama
  • 3,393
  • 8
  • 26
  • 38
  • 3
    Use List AddRange() and convert it to array. – vortex Mar 19 '16 at 07:54
  • from your example it looks like you are not looking for a range, just for a way to fill array in sequential chunks, and that's much easier I think. @vortex suggested right way for it – Konstantin Chernov Mar 19 '16 at 08:29
  • Possible duplicate of [How to copy part of an array to another array in C#?](http://stackoverflow.com/questions/733243/how-to-copy-part-of-an-array-to-another-array-in-c) – christian mini Mar 19 '16 at 08:57

5 Answers5

16

I think the closest you can do is via Array.Copy:

var s = new uint[64];
uint[] values = { 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200 };

int sourceIndex = 0;
int destinationIndex = 0;
Array.Copy(values, sourceIndex , s, destinationIndex , values.Length);
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
3

You could write an extension method to make this simpler:

public static class ArrayExt
{
    public static T[] Set<T>(this T[] self, int index, params T[] values)
    {
        Array.Copy(values, 0, self, index, values.Length);
        return self;
    }
}

Using it would look like this:

var s = new uint[64];
s.Set<uint>(0, 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200);

Because Set() returns the array, you can also chain calls like this:

s.Set<uint>( 0, 2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200)
 .Set<uint>(16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,  15,   16);

Note: The requirement to explicitly put the type into the call comes from the fact that we're using ints in the list of values.

You should really use uint since the destination is uint, then there is no need to specify the type:

var s = new uint[64];
s.Set(0, 2u, 4u, 6u, 3u, 1u, 7u, 8u, 9u, 7u, 11u, 37u, 32u, 19u, 16u, 178u, 2200u);

And of course it works with all types, e.g. string:

var x = new string[64];
x.Set(10, "A", "B", "C");
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

Not an exact match to the method you described, but you could create a List<int> and use the AddRange() method. Once you've added all the values, you can use to ToArray() method to turn it to an array.

var numbers = new List<int>();
numbers.AddRange(new List<int>{ 2, 4, 6, 3,  1, 7, 8, 9,  7, 11, 37, 32,  19, 16, 178, 2200 });
//numbers.AddRange(new List<int> { ... });

var asArray = numbers.ToArray();
Peter Szekeli
  • 2,712
  • 3
  • 30
  • 44
0

You can do it in a couple ways. Because I do not know you requirements and your code, I know you would need to modify this to suit you. But C# arrays are unlike C++ arrays, so the index always start from zero.

UInt64[] list = {2, 4, 6, 3, 1, 7, 8, 9, 7, 11, 37, 32, 19, 16, 178, 2200,..}; 
//Length of the list must be 31. That is contain 31 numbers.

UInt64[] s= new UInt64[31];

// You can have all the collection  

for (int i = 0; i < list.Length; i++)
{
    s[i] = list[i];
}    

// Or you can modify as below

for (int i = 0; i < 15; i++)
{
    s[i] = list[i];
}

for (int i = 16; i < 31; i++)
{
    s[i] = list[i];
}
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27
0

First you define your Arrays

    s1 := { ... }
    s2 := { ... }
    s3 := { ... }
    ...
    sn := { ... }

Then just concat them in a linq method chain

var result = s1.Concat(s2).Concat(s3)....Concat(sn);

You can do it in whatever order you like and get the exact result. It's easy to change the resulting sequence just by altering the chain slightly.

Konstantin Chernov
  • 1,899
  • 2
  • 21
  • 37