定义动态数组
如何在C#中定义动态数组?
C#不提供动态数组。 相反,它提供了以相同方式工作的List类。
要使用列表,请在文件顶部写入:
using System.Collections.Generic;
如果你想使用列表,写(字符串的例子):
List mylist = new List (); mylist.Add("First string in list");
如果需要调整数组大小,请查看Array.Resize 。
// Create and initialize a new string array. String[] myArr = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}; // Resize the array to a bigger size (five elements larger). Array.Resize(ref myArr, myArr.Length + 5); // Resize the array to a smaller size (four elements). Array.Resize(ref myArr, 4);
或者,您可以像其他人提到的那样使用List类。 如果您提前知道,请确保指定初始大小 ,以使列表不必在其下resize。 请参阅初始大小链接的备注部分。
List dinosaurs = new List (4); Console.WriteLine("nCapacity: {0}", dinosaurs.Capacity); dinosaurs.Add("Tyrannosaurus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Mamenchisaurus"); dinosaurs.Add("Deinonychus");
如果需要List中的数组,可以使用列表中的ToArray()函数。
string[] dinos = dinosaurs.ToArray();
C#确实提供了动态数组和动态数组操作。 数组的基础是动态的,可以使用变量进行修改。 你可以在这里找到arrays教程( https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx )。 我还包括演示空集数组和动态数组的代码,可以在运行时resize。
class Program { static void Main(string[] args) { int x = Convert.ToInt32(Console.ReadLine()); int y = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(x); { int[] dynamicArray1 = { };//empty array int[] numbers;//another way to declare a variable array as all arrays start as variable size numbers = new int[x];//setting this array to an unknown variable (will be user input) for (int tmpInt = 0; tmpInt < x; tmpInt++)//build up the first variable array (numbers) { numbers[tmpInt] = tmpInt; } Array.Resize(ref numbers,y);// resize to variable input dynamicArray1 = numbers;//set the empty set array to the numbers array size for (int z = 0; z < y; z++)//print to the new resize { Console.WriteLine(numbers[z].ToString());//print the numbers value Console.WriteLine(dynamicArray1[z].ToString());//print the empty set value } } Console.Write("Dynamic Arrays "); var name = Console.ReadLine(); } }
实际上你可以在C#中使用动态数组,这非常简单。 请记住,对上述问题的回答也是正确的,您可以声明List Generic创建动态数组的方法是声明您的Array为例
string[] dynamicArry1 = { };//notice I did not declare a size for the array List tmpList = new List (); int i = 1; for(int tmpInt = 0; tmpInt < 5; tmpInt++) { tmpList.Add("Testing, 1.0." + tmpInt + ", 200, 3.4" + tmpInt +"," + DateTime.Now.ToShortDateString()); //dynamicArry1[tmpInt] = new string[] { tmpList[tmpInt].ToCharArray() }; } dynamicArry1 = tmpList.ToArray();
怎么样的ArrayList?
如果我没错,ArrayList是动态数组的实现
在C#中定义动态数组的示例:
Console.WriteLine("Define Array Size? "); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter numbers:n"); int[] arr = new int[number]; for (int i = 0; i < number; i++) { arr[i] = Convert.ToInt32(Console.ReadLine()); } for (int i = 0; i < arr.Length; i++ ) { Console.WriteLine("Array Index: "+i + " AND Array Item: " + arr[i].ToString()); } Console.ReadKey();
像这样
上述就是C#学习教程:定义动态数组分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
int nSize = 17; int[] arrn = new int[nSize]; nSize++; arrn = new int[nSize];
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/936546.html