C# 定义动态数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1932554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:57:24  来源:igfitidea点击:

Defining Dynamic Array

c#.netarrays

提问by Harikrishna

How can I define a dynamic array in C#?

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

采纳答案by Alexander Shvetsov

C# doesn't provide dynamic arrays. Instead, it offers List class which works the same way.

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

To use lists, write at the top of your file:

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

using System.Collections.Generic;

And where you want to make use of a list, write (example for strings):

在你想使用列表的地方,写(例如字符串):

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

回答by AK_

like so

像这样

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

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

回答by SwDevMan81

Take a look at Array.Resizeif you need to resize an array.

如果需要调整数组大小,请查看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);

Alternatively you could use the List class as others have mentioned. Make sure you specify an initial sizeif you know it ahead of time to keep the list from having to resize itself underneath. See the remarks section of the initial size link.

或者,您可以使用其他人提到的 List 类。如果您提前知道它,请确保指定一个初始大小,以防止列表必须在下面调整自身大小。请参阅初始尺寸链接的备注部分。

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

If you need the array from the List, you can use the ToArray()function on the list.

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

    string[] dinos = dinosaurs.ToArray();

回答by MethodMan

Actually you can have Dynamic Arrays in C# it's very simple. keep in mind that the response to your question above is also correct you could declare a List Generic The way to Create a Dynamic Array would be to declare your Array for example

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

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

回答by Adam Right

how about ArrayList ?

ArrayList 怎么样?

If I'm not wrong ArrayList is an implementation of dynamic arrays

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

回答by cjjeeper

C# does provide dynamic arrays and dynamic array manipulation. The base of an array is dynamic and can be modified with a variable. You can find the array tutorial here (https://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx). I have also included code that demonstrates an empty set array and a dynamic array that can be resized at run time.

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

    }
}

回答by sydur.rahman21

Example of Defining Dynamic Array in C#:

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