7

I'm using the following statement in Java:

Arrays.fill(mynewArray, oldArray.Length, size, -1);

Please suggest the C# equivalent.

1

3 Answers 3

10

I don't know of anything in the framework which does that, but it's easy enough to implement:

// Note: start is inclusive, end is exclusive (as is conventional
// in computer science)
public static void Fill<T>(T[] array, int start, int end, T value)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (start < 0 || start >= end)
    {
        throw new ArgumentOutOfRangeException("fromIndex");
    }
    if (end >= array.Length)
    {
        throw new ArgumentOutOfRangeException("toIndex");
    }
    for (int i = start; i < end; i++)
    {
        array[i] = value;
    }
}

Or if you want to specify the count instead of the start/end:

public static void Fill<T>(T[] array, int start, int count, T value)
{
    if (array == null)
    {
        throw new ArgumentNullException("array");
    }
    if (count < 0)
    {
        throw new ArgumentOutOfRangeException("count");
    }
    if (start + count >= array.Length)
    {
        throw new ArgumentOutOfRangeException("count");
    }
    for (var i = start; i < start + count; i++)
    {
        array[i] = value;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I hope my edit is justified.. or otherwise name variable to uptoIndex
@nawfal: No, it was deliberately (although not consistently) exclusive. I've tidied it up and added an alternative.
Jon, why do you check for exceptional cases and throw yourself when the clr would do that? I'm a bit puzzled by this kind of design. Wouldn't only the second conditional check (in both your examples) alone suffice?
@nawfal: The CLR will throw different exceptions. Given that these are method arguments, the exceptions thrown should be ArgumentExceptions, IMO. That shows that it's the calling code at fault, not a problem within the method itself.
@JonSkeet, it is a little bit too late... still a thought: negative count will not cause an exception in the loop... throwing on negative count or on inverted start and stop needs to be well justified...
2

It seems like you would like to do something more like this

int[] bar = new int[] { 1, 2, 3, 4, 5 };
int newSize = 10;
int[] foo = Enumerable.Range(0, newSize).Select(i => i < bar.Length ? bar[i] : -1).ToArray();

Creating an new larger array with the old values and filling the extra.

For a simple fill try

int[] foo = Enumerable.Range(0, 10).Select(i => -1).ToArray();

or a sub range

int[] foo = new int[10];
Enumerable.Range(5, 9).Select(i => foo[i] = -1);

Comments

-2

Try like this

Array.Copy(source, target, 5);

For more information here

3 Comments

That doesn't do the same thing as fill.
but it is not target array to copy...it is taking length..mynewArray is int[]
Rasel's suggestion is ok. Working example: int[] arr=new int[1000]; Array.Copy(arr.Select (i => 5).ToArray(),arr, arr.Length);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.