将多个值放在数组中

时间:2021-10-11 21:41:40

I have a issue that i cant solve.

我有一个我无法解决的问题。

Here is the code

这是代码

 sik input = new sik();
        for (int i = 0; i < 5; i ++)
        {
            input.skId = securitiesArray[i].skId;
            input.country = securitiesArray[i].country;

        }
   sik[] inputs = new sik[]
            {
                input
            };

Now i know this will only put 1 value in the sik[] list.

现在我知道这只会在sik []列表中放入1个值。

How can i put all the 5 values in this list.

如何将所有5个值放在此列表中。

Thanks

谢谢

Note: i cannot initialize ski[] first. This has to be done in that order.

注意:我无法首先初始化ski []。这必须按顺序完成。

6 个解决方案

#1


4  

Any reason that it has to be an array?

它必须是一个数组的任何理由?

List<sik> input = new List<sik>();

for (int i = 0; i < 5; i ++)
{
    var newInput = new sik();        
    newInput.skId = securitiesArray[i].skId;
    newInput.country = securitiesArray[i].country;
    input.Add(newInput);
}

The reason that the List is useful is that it can dynamically grow with you, so you have no need to worry about how many instances you may need to add.

List有用的原因是它可以随你动态增长,因此您无需担心可能需要添加多少实例。

MSDN Documentation for List and all of it's glorious methods http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

列表的MSDN文档及其所有光荣的方法http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

#2


1  

    sik[] inputs = new sik[5];
    for (int i = 0; i < 5; i ++)
    {
        sik input = new sik();
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;
        inputs[i] = input;
    }

#3


1  

You can use Linq to do this.

您可以使用Linq执行此操作。

sik[] inputs = securitiesArray.Select(item => 
    new sik() 
    { 
        skId = item.skId, 
        country = item.country 
    }).ToArray();

#4


1  

You can't have variable size array, instead you can use List.

您不能使用可变大小的数组,而是可以使用List。

    List<sik> siks = new List<sik>();
    sik input = new sik();
    for (int i = 0; i < 5; i ++)
    {
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;

        siks.Add(input);
    }

If you want array yet, use sik[] inputs = skis.ToArray();

如果你还想要数组,请使用sik [] inputs = skis.ToArray();

#5


1  

You can also do this,

你也可以这样做,

List<sik> input=new List<sik>();
for(int i=0;i<securitiesArray.Length;i++)
{
   input.Add(new{skId=securitiesArray[i].skid,country=securitiesArray[i].country});
}

#6


1  

For what it's worth, here the Linq approach:

对于它的价值,这里是Linq的方法:

sik[] inputs = Enumerable.Range(0, 5)
    .Select(i => new sik{ kId = securitiesArray[i].skId, country = securitiesArray[i].country})
    .ToArray();

If securitiesArray is of type sik(the properties suggest), you can select directly from it:

如果securitiesArray的类型为sik(属性建议),您可以直接从中选择:

sik[] inputs = securitiesArray.Take(5).ToArray();

#1


4  

Any reason that it has to be an array?

它必须是一个数组的任何理由?

List<sik> input = new List<sik>();

for (int i = 0; i < 5; i ++)
{
    var newInput = new sik();        
    newInput.skId = securitiesArray[i].skId;
    newInput.country = securitiesArray[i].country;
    input.Add(newInput);
}

The reason that the List is useful is that it can dynamically grow with you, so you have no need to worry about how many instances you may need to add.

List有用的原因是它可以随你动态增长,因此您无需担心可能需要添加多少实例。

MSDN Documentation for List and all of it's glorious methods http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

列表的MSDN文档及其所有光荣的方法http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

#2


1  

    sik[] inputs = new sik[5];
    for (int i = 0; i < 5; i ++)
    {
        sik input = new sik();
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;
        inputs[i] = input;
    }

#3


1  

You can use Linq to do this.

您可以使用Linq执行此操作。

sik[] inputs = securitiesArray.Select(item => 
    new sik() 
    { 
        skId = item.skId, 
        country = item.country 
    }).ToArray();

#4


1  

You can't have variable size array, instead you can use List.

您不能使用可变大小的数组,而是可以使用List。

    List<sik> siks = new List<sik>();
    sik input = new sik();
    for (int i = 0; i < 5; i ++)
    {
        input.skId = securitiesArray[i].skId;
        input.country = securitiesArray[i].country;

        siks.Add(input);
    }

If you want array yet, use sik[] inputs = skis.ToArray();

如果你还想要数组,请使用sik [] inputs = skis.ToArray();

#5


1  

You can also do this,

你也可以这样做,

List<sik> input=new List<sik>();
for(int i=0;i<securitiesArray.Length;i++)
{
   input.Add(new{skId=securitiesArray[i].skid,country=securitiesArray[i].country});
}

#6


1  

For what it's worth, here the Linq approach:

对于它的价值,这里是Linq的方法:

sik[] inputs = Enumerable.Range(0, 5)
    .Select(i => new sik{ kId = securitiesArray[i].skId, country = securitiesArray[i].country})
    .ToArray();

If securitiesArray is of type sik(the properties suggest), you can select directly from it:

如果securitiesArray的类型为sik(属性建议),您可以直接从中选择:

sik[] inputs = securitiesArray.Take(5).ToArray();