简体   繁体   English

通过循环创建一个数组

[英]create an array via loop

I need to create an array of strings, easy enough... the only problem is that the strings are integers from 1-1000 and I really dont want to type each of them. 我需要创建一个字符串数组,这很容易......唯一的问题是字符串是1-1000的整数,我真的不想输入它们中的每一个。

Can you create a loop that could create this? 你能创建一个可以创建它的循环吗?

right now it looks like this 现在它看起来像这样

        private readonly string[] _myArray = { "1", "2", "3", "4", "5" };

and its called by 它的召唤

        for (var i = 0; i < _myArray.Length; i++)
        {
            arFoo[i].SetBar(_myArray[i]);
        }

any suggestions on how to add the other 995 without manually typing them? 关于如何在不手动输入的情况下添加其他995的任何建议?

This is simple and clean: 这很简单干净:

readonly string[] _myArray 
    = Enumerable.Range(1, 1000)
        .Select(i => i.ToString())
        .ToArray();

If you want to use LINQ: 如果你想使用LINQ:

    private readonly string[] _myArray;

    public Foo()
    {
        _myArray = Enumerable.Range(1, 1000).Select(s => s.ToString()).ToArray();
    }

Or more traditionally: 或更传统的:

public class Foo
{
    private readonly string[] _myArray;

    public Foo()
    {
        _myArray = new string[1000];
        for(int i=1; i<=1000; i++)
        {
            _myArray[i - 1] = i.ToString();
        }
    }
}
_myArray = new string[1000];
for (int i = 1; i <= 1000; i++) _myArray[i - 1] = i.ToString();
string[] arr = new string[1000];
for (int i = 1; i <= 1000; i++)
{
    arr[i-1] = i.ToString();
}

How about 怎么样

int NumberOfElements = 1000;
String[] Array = new String[NumberOfElements];

for(int i=0; i<Array.Length; i++)
{
   Array[i] = (i + 1).ToString();
}

Do you need an array? 你需要一个阵列吗? You could just do this: 你可以这样做:

for (var i = 0; i < 1000; i++)       
{            
   arFoo[i].SetBar(i.ToString()); 
}

If you do need an array, understand that arrays in C# (and in .Net) are fixed-size. 如果确实需要数组,请理解C#(和.Net)中的数组是固定大小的。 You would need another data structure, like a List<String> in order to add elements, then you can transform to an array (if truly needed) via ToArray() . 您需要另一个数据结构,如List<String>以添加元素,然后您可以通过ToArray()转换为数组(如果真的需要ToArray()

var array = Enumerable.Range(1, 1000).Select(item => item.ToString()).ToArray();
int[] arr = Enumerable.Range(1, 1000).ToArray();

You can simply do it in a for loop as follows, calling ToString on the int 'i' 您可以在for循环中执行以下操作,在int'i'上调用ToString

private string[] _myArray = new string[1000];

for(int i=0;i<1000;i++)
{
  _myArray[i] = i.ToString();
}
private readonly string[] _myArray = new string[1000];

for (int i = 0; i < _myArray.Length; i++)
    _myArray[i] = i.ToString();

This is method that will generate array of string from 'from' to 'to' inclusive. 这种方法将生成从“从”到“包含”的字符串数组。

private string[] GetNumbers(int from, int to)
{
  int size = to - from + 1;
  string[] result = new string[size];
  int number = from;
  for (int i = 0; i < size; i++)
  {
    result[i] = number.ToString();
    number++;
  }
  return result;
}

string[] numbers = GetNumbers(1, 1000); // to get what You want

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM