这可能是一个非常简单的方法-我从C#开始,需要向数组添加值,例如:
1
2
3
4
5
6int[] terms; for(int runs = 0; runs < 400; runs++) { terms[] = runs; }
对于那些使用PHP的人,这是我要在C#中尝试做的事情:
1
2
3
4
5$arr = array(); for ($i = 0; $i < 10; $i++) { $arr[] = $i; }
#1楼
1
2
3
4
5
6
7
8
9
10
11
12
13
14int[] 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
* /
#2楼
您必须先分配数组:
1
2
3
4
5
6int [] terms = new int[400]; // allocate an array of 400 ints for(int runs = 0; runs < terms.Length; runs++) // Use Length property rather than the 400 magic number again { terms[runs] = value; }
#3楼
1
2
3
4
5
6int[] terms = new int[400]; for(int runs = 0; runs < 400; runs++) { terms[runs] = value; }
#4楼
1
2
3
4
5
6
7int ArraySize = 400; int[] terms = new int[ArraySize]; for(int runs = 0; runs < ArraySize; runs++) { terms[runs] = runs; }
那就是我编写代码的方式。
#5楼
您不能只是轻松地将元素添加到数组。 您可以将元素设置为下降888概述的给定位置,但是我建议改用List<int>
或Collection<int>
,如果需要将其转换为数组,请使用ToArray()
。
#6楼
你可以这样-
1
2
3
4
5
6int[] terms = new int[400]; for (int runs = 0; runs < 400; runs++) { terms[runs] = value; }
另外,您也可以使用列表-列表的优势在于,实例化列表时无需知道数组大小。
1
2
3
4
5
6
7
8List<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();
编辑: a) 用于对列表<T>循环是有点超过2倍价格比foreach循环上表<T>,B)循环上阵列大约比循环上表<T>,c)中循环上更便宜的2倍使用for的数组比使用foreach在List <T>上循环(我们大多数人这样做)便宜5倍。
#7楼
C#数组是固定长度的,并且总是索引的。 使用Motti的解决方案:
1
2
3
4
5
6int [] terms = new int[400]; for(int runs = 0; runs < 400; runs++) { terms[runs] = value; }
请注意,此数组是一个密集数组,是一个400字节的连续块,您可以在其中放置东西。 如果要动态调整大小的数组,请使用List <int>。
1
2
3
4
5
6List<int> terms = new List<int>(); for(int runs = 0; runs < 400; runs ++) { terms.Add(runs); }
int []和List <int>都不是关联数组-在C#中将是Dictionary <>。 数组和列表都是密集的。
#8楼
这里提供了如何使用数组的答案。
但是,C#有一个非常方便的东西,叫做System.Collections :)
集合是使用数组的理想选择,尽管其中许多在内部使用数组。
例如,C#有一个名为List的集合,其功能与PHP数组非常相似。
1
2
3
4
5
6
7
8using 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); }
#9楼
如果您使用C#3编写代码,则可以使用单行代码进行:
1
2int[] terms = Enumerable.Range(0, 400).ToArray();
此代码段假定您在文件顶部具有System.Linq的using指令。
另一方面,如果您正在寻找可以动态调整大小的东西(PHP似乎是这种情况(我从没真正学过)),那么您可能想使用List而不是int [] 。 该代码如下所示:
1
2List<int> terms = Enumerable.Range(0, 400).ToList();
但是请注意,您不能通过将term [400]设置为一个值来简单地添加第401个元素。 相反,您需要像这样调用Add():
1
2terms.Add(1337);
#10楼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19static 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.*/ }
#11楼
1
2
3
4
5
6
7
8
9
10/*arrayname is an array of 5 integer*/ int[] arrayname = new int[5]; int i, j; /*initialize elements of array arrayname*/ for (i = 0; i < 5; i++) { arrayname[i] = i + 100; }
#12楼
只是一种不同的方法:
1
2
3
4
5
6
7int runs = 0; bool batting = true; string scorecard; while (batting = runs < 400) scorecard += "!" + runs++; return scorecard.Split("!");
#13楼
如果您确实需要数组,那么以下可能是最简单的:
1
2
3
4
5
6
7
8
9using 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); } int [] terms = list.ToArray();
#14楼
如果您不知道数组的大小,或者已经有要添加的现有数组。 您可以通过两种方式解决此问题。 第一种是使用通用List<T>
:为此,您需要将数组转换为var termsList = terms.ToList();
并使用Add方法。 然后,在完成后使用var terms = termsList.ToArray();
转换回数组的方法。
1
2
3
4
5
6var 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();
第二种方法是调整当前数组的大小:
1
2
3
4
5
6
7
8
9
10var 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
#15楼
正如其他人所描述的那样,将List用作中介是最简单的方法,但是由于您的输入是一个数组,并且您不只是想将数据保存在List中,所以我想您可能会担心性能。
最有效的方法可能是分配一个新数组,然后使用Array.Copy或Array.CopyTo。 如果您只想在列表末尾添加项目,这并不难:
1
2
3
4
5
6
7
8
9
10
11
12public 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次。
#16楼
基于Thracx的答案(我没有足够的分数来回答):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public 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; }
这允许向数组中添加多个项,或者仅将数组作为参数传递以连接两个数组。
#17楼
使用Linq的方法Concat可以简化此过程
1
2
3int[] array = new int[] { 3, 4 }; array = array.Concat(new int[] { 2 }).ToArray();
结果3,4,2
#18楼
我将其添加为另一个变体。 我更喜欢这种类型的功能编码线。
1
2Enumerable.Range(0, 400).Select(x => x).ToArray();
最后
以上就是传统烤鸡最近收集整理的关于向C#数组添加值的全部内容,更多相关向C#数组添加值内容请搜索靠谱客的其他文章。
发表评论 取消回复