这可能是一个非常简单的问题--我刚开始接触C#,需要向一个数组添加数值,比如说。
int[] terms;
for(int runs = 0; runs < 400; runs++)
{
terms[] = runs;
}
对于那些使用过PHP的人来说,这就是我在C#中试图做的事情。
$arr = array();
for ($i = 0; $i < 10; $i++) {
$arr[] = $i;
}
你可以这样做 -
int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
另外,你也可以使用 Lists --List 的好处是,你不需要在实例化 list 时知道数组的大小。
List<int> termsList = new List<int>();
for (int runs = 0; runs < 400; runs++)
{
termsList.Add(value);
}
// You can convert it back to an array if you would like to
int[] terms = termsList.ToArray();
如果你是用C# 3写的,你可以用单行本来做。
int[] terms = Enumerable.Range(0, 400).ToArray();
这段代码假设你在文件的顶部有一个System.Linq的使用指令。
另一方面,如果你正在寻找可以动态调整大小的东西,就像 PHP 的情况一样(我从未真正了解过),那么你可能想要使用 List
List<int> terms = Enumerable.Range(0, 400).ToList();
但是,请注意,你不能简单地通过将 terms[400]设置为一个值来添加第 401 个元素。 你需要调用 Add() ,就像这样。
terms.Add(1337);
使用[Linq][1]'的方法[Concat][2]使之简单化。
int[] array = new int[] { 3, 4 };
array = array.Concat(new int[] { 2 }).ToArray();
结果 3,4,2
[1]: https://docs.microsoft.com/en-us/dotnet/api/system.linq [2]: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.concat
这里提供了如何使用数组的答案。
然而,C#有一个非常方便的东西叫做System.Collections:)
集合是使用数组的高级替代品,尽管它们中的许多都在内部使用数组。
例如,C#有一个名为List的集合,它的功能与PHP数组非常相似。
using System.Collections.Generic;
// Create a List, and it can only contain integers.
List<int> list = new List<int>();
for (int i = 0; i < 400; i++)
{
list.Add(i);
}
根据Thracx的回答(我没有足够的分数来回答)。
public static T[] Add<T>(this T[] target, params T[] items)
{
// Validate the parameters
if (target == null) {
target = new T[] { };
}
if (items== null) {
items = new T[] { };
}
// Join the arrays
T[] result = new T[target.Length + items.Length];
target.CopyTo(result, 0);
items.CopyTo(result, target.Length);
return result;
}
这允许在数组中添加不止一个项目,或者只是传递一个数组作为参数来连接两个数组。
使用List<T>.作为中介是最简单的方法,正如其他人所描述的那样。 作为中介是最简单的方法,正如其他人所描述的那样,但是由于你的输入是一个数组,而且你不只是想把数据保存在List中,我猜你可能会担心性能问题。
最有效的方法可能是分配一个新的数组,然后使用Array.Copy或Array.CopyTo。 如果你只是想在列表的最后添加一个项目,这并不难。
public static T[] Add<T>(this T[] target, T item)
{
if (target == null)
{
//TODO: Return null or throw ArgumentNullException;
}
T[] result = new T[target.Length + 1];
target.CopyTo(result, 0);
result[target.Length] = item;
return result;
}
如果需要的话,我还可以贴出一个Insert扩展方法的代码,这个方法把目标索引作为输入。 它'比较复杂,使用静态方法Array.Copy 1-2次。
C#数组是固定长度的,而且总是有索引。 采用Motti'的解决方案。
int [] terms = new int[400];
for(int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
请注意,这个数组是一个密集数组,是一个400字节的连续块,你可以在这里放下东西。 如果你想要一个动态大小的数组,请使用List<int>。
List<int> terms = new List<int>();
for(int runs = 0; runs < 400; runs ++)
{
terms.Add(runs);
}
无论是int[]还是List<int> 是一个关联数组--那将是一个Dictionary<> 在C#中,它是一个Dictionary<>。 数组和列表都是密集的。
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
*/。
你不能简单地将一个元素添加到数组中。
你可以像_fallen888_概述的那样,在给定的位置设置元素,但我建议使用List<int>
或Collection<int>
来代替,如果你需要将其转换为数组,则使用ToArray()
。
只是方法不同而已。
int runs = 0;
bool batting = true;
string scorecard;
while (batting = runs < 400)
scorecard += "!" + runs++;
return scorecard.Split("!");
如果你不知道数组的大小,或者已经有一个现有的数组,你正在添加到。
你可以用两种方式去做。
第一种是使用一个通用的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>
对象时受到的冲击更大。
时间 单位为ticks:。
3项
阵列调整时间: 6
List Add Time: 16
400件
阵列调整时间:305
List Add Time: 20
static void Main(string[] args)
{
int[] arrayname = new int[5];/*arrayname is an array of 5 integer [5] mean in array [0],[1],[2],[3],[4],[5] because array starts with zero*/
int i, j;
/*initialize elements of array arrayname*/
for (i = 0; i < 5; i++)
{
arrayname[i] = i + 100;
}
/*output each array element value*/
for (j = 0; j < 5; j++)
{
Console.WriteLine("Element and output value [{0}]={1}",j,arrayname[j]);
}
Console.ReadKey();/*Obtains the next character or function key pressed by the user.
The pressed key is displayed in the console window.*/
}