这可能是一个非常简单的问题 - 我刚开始学习C#,需要向数组中添加值,例如:
int[] terms;
for(int runs = 0; runs < 400; runs++)
{
terms[] = runs;
}
如果您使用过PHP,这是我在C#中想要做的事情:
$arr = array();
for ($i = 0; $i < 10; $i++) {
$arr[] = $i;
}
你不能轻易地向数组中添加元素。如fallen888所述,你可以设置给定位置的元素,但我建议使用List<int>
或Collection<int>
,如果需要将其转换为数组,则使用ToArray()
。
C# 数组长度固定且始终有索引。采用 Motti 的解决方案:
int [] terms = new int[400];
for(int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
List<int> terms = new List<int>();
for(int runs = 0; runs < 400; runs ++)
{
terms.Add(runs);
}
int[]和List<int>都不是关联数组,这种类型的数组在C#中被称为Dictionary<>。数组和列表都是密集型的数据结构。
一种方法是通过LINQ来填充数组。
如果你想要用一个元素来填充数组,你可以简单地写成:
string[] arrayToBeFilled;
arrayToBeFilled= arrayToBeFilled.Append("str").ToArray();
//the array you want to fill values in
string[] arrayToBeFilled;
//list of values that you want to fill inside an array
List<string> listToFill = new List<string> { "a1", "a2", "a3" };
//looping through list to start filling the array
foreach (string str in listToFill){
// here are the LINQ extensions
arrayToBeFilled= arrayToBeFilled.Append(str).ToArray();
}
public void ArrayPush<T>(ref T[] table, object value)
{
Array.Resize(ref table, table.Length + 1); // Resizing the array for the cloned length (+-) (+1)
table.SetValue(value, table.Length - 1); // Setting the value for the new element
}
var usageList = usageArray.ToList();
usageList.Add("newstuff");
usageArray = usageList.ToArray();
我将添加这个变量的另一种形式。我更喜欢这种类型的功能性编码。
Enumerable.Range(0, 400).Select(x => x).ToArray();
int[] terms = new int[10]; //create 10 empty index in array terms
//fill value = 400 for every index (run) in the array
//terms.Length is the total length of the array, it is equal to 10 in this case
for (int run = 0; run < terms.Length; run++)
{
terms[run] = 400;
}
//print value from each of the index
for (int run = 0; run < terms.Length; run++)
{
Console.WriteLine("Value in index {0}:\t{1}",run, terms[run]);
}
Console.ReadLine();
索引0的值:400
索引1的值:400
索引2的值:400
索引3的值:400
索引4的值:400
索引5的值:400
索引6的值:400
索引7的值:400
索引8的值:400
索引9的值:400
*/
如果您不知道数组的大小或已经有一个现有的数组需要添加元素。您可以通过两种方式来实现。第一种是使用通用的List<T>
:
要实现这个,您需要将数组转换为var termsList = terms.ToList();
,然后使用Add方法添加元素。完成后使用var terms = termsList.ToArray();
方法将其转换回数组。
var terms = default(int[]);
var termsList = terms == null ? new List<int>() : terms.ToList();
for(var i = 0; i < 400; i++)
termsList.Add(i);
terms = termsList.ToArray();
var terms = default(int[]);
for(var i = 0; i < 400; i++)
{
if(terms == null)
terms = new int[1];
else
Array.Resize<int>(ref terms, terms.Length + 1);
terms[terms.Length - 1] = i;
}
如果你正在使用.NET 3.5,则可以使用Array.Add(...);
来动态添加元素。
这两种方式都能够实现动态添加元素。如果你需要添加大量的元素,那么建议你使用List<T>
。但是如果只是添加一些元素,那么调整数组大小可能会有更好的性能。这是因为创建List<T>
对象所需的资源会更多。
时间(以滴答为单位):
3个元素
数组调整大小时间:6
列表添加时间:16
400个元素
数组调整大小时间:305
列表添加时间:20
List<int> termsLst=new List<int>();
for (int runs = 0; runs < 400; runs++)
{
termsLst.Add(runs);
}
int[] terms = termsLst.ToArray();
List<int> termsLst = terms.ToList();
for (int runs = 0; runs < 400; runs++)
{
termsLst.Add(runs);
}
terms = termsLst.ToArray();
注意:不要忘记在文件开头添加using System.Linq;。
一种不同的方法:
int runs = 0;
bool batting = true;
string scorecard;
while (batting = runs < 400)
scorecard += "!" + runs++;
return scorecard.Split("!");
List<int>
这样的集合(这是你应该在这里使用的),但是当你请求一个数组时,它实际上会给你一个真正的数组。 - Joel Coehoorn