通过循环创建数组

发布于 2024-11-08 07:41:56 字数 401 浏览 0 评论 0原文

我需要创建一个字符串数组,很简单...唯一的问题是字符串是 1-1000 之间的整数,我真的不想键入每个字符串。

你能创建一个可以创建这个的循环吗?

现在它看起来像这样

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

,并且有

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

任何有关如何添加其他 995 而无需手动输入的建议来调用它吗?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

鼻尖触碰 2024-11-15 07:41:56

这很简单又干净:

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

This is simple and clean:

readonly string[] _myArray 
    = Enumerable.Range(1, 1000)
        .Select(i => i.ToString())
        .ToArray();
岛徒 2024-11-15 07:41:56

如果您想使用 LINQ:

    private readonly string[] _myArray;

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

或更传统的方式:

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();
        }
    }
}

If you want to use 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();
        }
    }
}
⊕婉儿 2024-11-15 07:41:56

怎么样

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

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

How about

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

for(int i=0; i<Array.Length; i++)
{
   Array[i] = (i + 1).ToString();
}
反话 2024-11-15 07:41:56

你需要一个数组吗?您可以这样做:

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

如果您确实需要数组,请了解 C#(和 .Net)中的数组是固定大小的。您需要另一个数据结构,例如 List 来添加元素,然后您可以通过 ToArray() 转换为数组(如果确实需要)。

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. 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().

反话 2024-11-15 07:41:56
var array = Enumerable.Range(1, 1000).Select(item => item.ToString()).ToArray();
var array = Enumerable.Range(1, 1000).Select(item => item.ToString()).ToArray();
明月松间行 2024-11-15 07:41:56
int[] arr = Enumerable.Range(1, 1000).ToArray();
int[] arr = Enumerable.Range(1, 1000).ToArray();
一百个冬季 2024-11-15 07:41:56
_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();
你的笑 2024-11-15 07:41:56
string[] arr = new string[1000];
for (int i = 1; i <= 1000; i++)
{
    arr[i-1] = i.ToString();
}
string[] arr = new string[1000];
for (int i = 1; i <= 1000; i++)
{
    arr[i-1] = i.ToString();
}
夏九 2024-11-15 07:41:56

您可以简单地在 for 循环中执行此操作,如下所示,在 int 'i' 上调用 ToString

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

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

You can simply do it in a for loop as follows, calling ToString on the int 'i'

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

for(int i=0;i<1000;i++)
{
  _myArray[i] = i.ToString();
}
再可℃爱ぅ一点好了 2024-11-15 07:41:56
private readonly string[] _myArray = new string[1000];

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

for (int i = 0; i < _myArray.Length; i++)
    _myArray[i] = i.ToString();
拥醉 2024-11-15 07:41:56

该方法将生成从“from”到“to”(包含在内)的字符串数组。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文