定义动态数组

时间:2009-12-19 10:27:01

标签: c# .net arrays

如何在C#中定义动态数组?

7 个答案:

答案 0 :(得分:15)

C#不提供动态数组。相反,它提供了以相同方式工作的List类。

要使用列表,请在文件顶部写下:

using System.Collections.Generic;

如果您想使用列表,请编写(字符串示例):

List<string> mylist = new List<string>();
mylist.Add("First string in list");

答案 1 :(得分:8)

如果需要调整数组大小,请查看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类。如果您提前知道,请确保指定initial size,以使列表不必在下面调整自身大小。请参阅初始大小链接的备注部分。

    List<string> dinosaurs = new List<string>(4);

    Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

    dinosaurs.Add("Tyrannosaurus");
    dinosaurs.Add("Amargasaurus");
    dinosaurs.Add("Mamenchisaurus");
    dinosaurs.Add("Deinonychus");

如果您需要列表中的数组,可以使用列表中的ToArray()函数。

    string[] dinos = dinosaurs.ToArray();

答案 2 :(得分:1)

C#确实提供动态数组和动态数组操作。数组的基础是动态的,可以使用变量进行修改。您可以在此处找到阵列教程(https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx)。我还包括演示空集数组和动态数组的代码,可以在运行时调整大小。

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();

    }
}

答案 3 :(得分:0)

实际上你可以在C#中使用动态数组,这非常简单。 请记住,您对上述问题的回答也是正确的,您可以声明一个 列表通用 创建动态数组的方法是声明您的数组,例如

        string[] dynamicArry1 = { };//notice I did not declare a size for the array
        List<String> tmpList = new List<string>();
        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();

答案 4 :(得分:0)

ArrayList怎么样?

如果我没错,ArrayList是动态数组的实现

答案 5 :(得分:0)

在C#中定义动态数组的示例:

wrap.innerHTML += `<div class="newData">${Title}</div>`;

答案 6 :(得分:-1)

喜欢这样

int nSize = 17;
int[] arrn = new int[nSize];

nSize++;
arrn = new int[nSize];
相关问题