我在Java中使用以下语句:
Arrays.fill(mynewArray, oldArray.Length, size, -1);
Run Code Online (Sandbox Code Playgroud)
请建议等效的C#.
Jon*_*eet 10
我不知道框架中有什么做到这一点,但它很容易实现:
// 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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果要指定计数而不是开始/结束:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7710 次 |
| 最近记录: |