在 C# 中填充数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14919234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:26:51  来源:igfitidea点击:

fill an array in C#

c#arrays

提问by Mishax

In Java there are about 18 a static "fill" methods in the class Arrays that serve to assign one value to each element in an array. I'm looking for an equivalent in C# to achieve the same thing but I'm not finding anything with the same compactness:

在 Java 中,Arrays 类中有大约 18 个静态“填充”方法,用于为数组中的每个元素分配一个值。我正在寻找 C# 中的等价物来实现相同的目的,但我没有找到任何具有相同紧凑性的东西:

1) ForEach as I understand passes elements in the array by value so I can't change them

1)据我所知,ForEach 按值传递数组中的元素,因此我无法更改它们

2) Repeat from Enumerable creates a new array, it seems to be overhead to create a new array and then copy each element from the array

2) Repeat from Enumerable 创建一个新数组,创建一个新数组然后从数组中复制每个元素似乎是开销

3) a for-loop isn't pretty, which I guess is why the Java folks introduced this compact notation to begin with

3) for 循环并不漂亮,我想这就是 Java 人员引入这种紧凑符号的原因

4) The Clear method from the Array class can set everything to zero, but then how do I convert zero to the non-zero value I want?

4) Array 类的 Clear 方法可以将所有内容都设置为零,但是如何将零转换为我想要的非零值?

To demonstrate Java's syntax consider this code that prints three times the number 7:

为了演示 Java 的语法,请考虑打印三倍于数字 7 的代码:

int[] iii = {1, 2, 3};
Arrays.fill(iii, 7);
for (int i : iii) {
    System.out.println(i);
}

采纳答案by bradgonesurfing

Write yourself an extension method

自己写一个扩展方法

public static class ArrayExtensions {
    public static void Fill<T>(this T[] originalArray, T with) {
        for(int i = 0; i < originalArray.Length; i++){
            originalArray[i] = with;
        }
    }  
}

and use it like

并像使用它一样

int foo[] = new int[]{0,0,0,0,0};
foo.Fill(13);

will fill all the elements with 13

将用 13 填充所有元素

回答by Igoy

You could try something like this:

你可以尝试这样的事情:

I have initialzed the array for having value 5, you could put your number similarly.

我已经初始化了具有值 5 的数组,您可以类似地输入您的数字。

int[] arr = new int[10]; // your initial array

arr = arr.Select(i => 5).ToArray(); // array initialized to 5.

回答by WannaCSharp

you could write an extension method

你可以写一个扩展方法

public static void Fill<T>(this T[] array, T value)
{
  for(int i = 0; i < array.Length; i++) 
  {
     array[i] = value;
  }
}

回答by nawfal

public static void Fill<T>(this IList<T> col, T value, int fromIndex, int toIndex)
{
    if (fromIndex > toIndex)
        throw new ArgumentOutOfRangeException("fromIndex");

    for (var i = fromIndex; i <= toIndex; i++)
        col[i] = value;
}

Something that works for all IList<T>s.

适用于所有IList<T>s 的东西。

回答by roadrunner66

Say you want to fill with number 13.

假设你想用数字 13 填充。

int[] myarr = Enumerable.Range(0, 10).Select(n => 13).ToArray();

int[] myarr = Enumerable.Range(0, 10).Select(n => 13).ToArray();

or

或者

List<int> myarr = Enumerable.Range(0,10).Select(n => 13).ToList();

List<int> myarr = Enumerable.Range(0,10).Select(n => 13).ToList();

if you prefer a list.

如果您更喜欢列表。

回答by Alex Shtof

int[] arr = Enumerable.Repeat(42, 10000).ToArray();

I believe that this does the job :)

我相信这可以完成工作:)