稍后在程序中调整 C# 中的数组大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19328229/
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-10 14:45:49  来源:igfitidea点击:

Resize array in C# later in the program

c#arrays

提问by user613326

I am not sure if this is possible in c# but i'm having a variable defined in

我不确定这在 c# 中是否可行,但我在中定义了一个变量

 public partial class Form1 : Form
 {...
   ...
    #region used variables
    public ulong[] logP = {0,0,0,0,0,0,0,0}
  ....
  ..

Then later in the program i want have an option to adjust the size in the program before the main routines will launch over it and do some calculations

然后在程序的后面,我想有一个选项来调整程序中的大小,然后主要例程将启动它并进行一些计算

Because i like to test with vaious sets of numbers and array sizes i would like to have an option to resize the array, for each test (but this array isnt bound to a single procedure, its a global variable that needs to be resizable.

因为我喜欢用不同的数字和数组大小集进行测试,所以我想有一个选项来调整数组的大小,对于每个测试(但这个数组没有绑定到单个过程,它是一个需要调整大小的全局变量。

As the whole program at various parts is using this array, (under various functions) how should i put this part

由于各个部分的整个程序都在使用这个数组,(在各种功能下)我应该如何放置这部分

    ulong [] sumP = new ulong[numericupdown.valeu];

So that it would change this global variable size ?

所以它会改变这个全局变量大小?

采纳答案by Moho

You cannot resize an array; you must declare a new array of the desired size and copy the contents of the original array into the new array.

您不能调整数组的大小;您必须声明一个所需大小的新数组,并将原始数组的内容复制到新数组中。

Update: I don't like Array.Resize- it doesn't resize the array (as the method name would suggest), it creates a new array and replaces the reference:

更新:我不喜欢Array.Resize- 它不会调整数组的大小(正如方法名称所暗示的那样),它会创建一个新数组并替换引用:

    static void Main(string[] args)
    {
        int[] a1 = new int[] { 1, 2, 3 };
        int[] a2 = a1;

        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 3
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3
        Array.Resize(ref a1, 10);
        Console.WriteLine("A1 Length: {0}", a1.Length); // will be 10
        Console.WriteLine("A2 Length: {0}", a2.Length); // will be 3 - not good

        Console.ReadLine();
    }

回答by Ahmed ilyas

as icemanind suggested - consider using a List array however as better practice, make sure you initialize the array capacity to a reasonable amount i.e:

正如 icemanind 建议的那样 - 考虑使用 List 数组,但作为更好的做法,请确保将数组容量初始化为合理的数量,即:

List<string> myList = new List<string>(10); // initial capacity is 10

the advantage is performance and space allocation is predefined and it doesn't mean it needs to recreate the collection everytime you add an item to it as it would use the existing allocated amount until its reached then it increases it internally.

优点是性能和空间分配是预定义的,这并不意味着每次向其中添加项目时都需要重新创建集合,因为它会使用现有的分配量,直到达到它然后在内部增加它。

回答by Kyle

Use Array.Resize()to resize an array,

使用Array.Resize()调整大小的数组,

http://msdn.microsoft.com/en-us/library/bb348051.aspx

http://msdn.microsoft.com/en-us/library/bb348051.aspx

For example:

例如:

        var intArray = new int[5] { 1, 2, 3, 4, 5 };

        Array.Resize(ref intArray, 10);

回答by GANI

    class Program
    {
       static void Main(string[] args)
       {
        var myArr = new int[5] { 1, 2, 3, 4, 5 };
        Array.Resize(ref myArr, 10);
        for (int i = 0; i < myArr.Length; i++)
        {
            Console.WriteLine("   [{0}] : {1}", i, myArr[i]);
        }
        Console.ReadLine();
    }
}