如何添加到数组C#的末尾?

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

How to add to end of array C#?

c#.netwinformsarrays

提问by Timmy

How do I add a new itemfrom a TextBox and Button on a Windows Form at the end of an ArrayListwhich references a class?

如何itemArrayList引用类的末尾添加来自 Windows 窗体上的文本框和按钮的新内容?

private product[] value = new product[4];

value[1] = new product("One",5)
value[2] = new product("Two",3)
value[3] = new product("Three",8)

Workflow

工作流程

  • Enter new products details into textbox1, textbox2, textbox3
  • When I click Addthe new product gets added to the array:

    value[1] = new product("One",5)
    value[2] = new product("Two",3)
    value[3] = new product("Three",8)
    value[4] = new product("Four",2)

  • 将新产品详细信息输入textbox1, textbox2,textbox3
  • 当我点击Add新产品被添加到数组时:

    value[1] = new product("One",5)
    value[2] = new product("Two",3)
    value[3] = new product("Three",8)
    value[4] = new product("Four",2)

What is the code for doing this?

这样做的代码是什么?

回答by John Weldon

Arrays are zero indexed, so an array initialized to a size of 4 could only be accessed up to index 3...

数组是零索引的,因此初始化为 4 大小的数组只能访问索引 3 ...

If you wanted the array to grow, then you'd have to either initialize the array at least as large as you wanted to be able to grow to, or else you'd have to create a new array with the new larger size and copy the old array over; not very efficient.

如果您希望数组增长,那么您必须至少将数组初始化为您希望能够增长到的大小,否则您必须创建一个具有新的更大大小的新数组并复制旧数组结束;效率不高。

In this case, you'd do better to use a collection like a list, rather than an array, so that the size can dynamically increase.

在这种情况下,最好使用列表之类的集合,而不是数组,这样大小可以动态增加。

回答by Pop Catalin

Arrays are fixed size, which means you can't add more elements than the number allocated at creation time, if you need a auto sizing collection you could use List<T>or an ArrayList

数组是固定大小的,这意味着您不能添加比创建时分配的数量更多的元素,如果您需要可以使用的自动调整大小的集合List<T>ArrayList

Example:

例子:

// using collection initializers to add two products at creation time
List<Product> products = new List<Product>{new Product("One",5), new Product("Two",3) };

// then add more elements as needed
products.Add(new Product("Three",8));

回答by Russ Cam

I think you need a List<product>collection, looking at your code. Then just call the Add()method on it

我认为你需要一个List<product>集合,看看你的代码。然后就调用Add()方法就可以了

回答by s_hewitt

Use List as other people mentioned. If you are set on arrays, use

使用其他人提到的列表。如果您在数组上设置,请使用

Array.Resize<Product>(ref product, your new size);

If you're only going to be adding a couple of products (over the lifetime of your array) just do something like

如果您只打算添加几个产品(在阵列的整个生命周期内),请执行以下操作

Array.Resize<Product>(ref product, product.Length + 1);

If you are going to be adding a lot of products, you might want to do something similar to what List does - like this:

如果您要添加很多产品,您可能想要做一些类似于 List 所做的事情 - 像这样:

Array.Resize<Product>(ref product, product.Length * 2);

回答by Guffa

You can't add items to an array, you would have to create a new array that is larger and copy the items to it. There is a method for that, which is somewhat misleadingly named Resizeas it doesn't actually resize the array:

您不能将项目添加到数组中,您必须创建一个更大的新数组并将项目复制到其中。有一种方法,它的名称有点误导,Resize因为它实际上并没有调整数组的大小:

Array.Resize<product>(ref value, 5);

If you want to add items to a collection, you should use a List instead:

如果要将项目添加到集合中,则应改用 List:

private List<product> value = new List<product>();
value.Add(new product("One",5));
value.Add(new product("Two",3));
value.Add(new product("Three",8));

value.Add(new product("Four",2));

Edit:
If you want to resize the array, you might want to increase the size instead of resizing it to a specific value:

编辑:
如果要调整数组大小,您可能希望增加大小而不是将其调整为特定值:

int index = value.Length;
Array.Resize<product>(ref value, index + 1);
value[index] = ...

回答by Sune Rievers

You should probably also take a look at Array versus List: When to use which?

您可能还应该看看数组与列表:何时使用哪个?

回答by Mohamed Eissa

Array.Resize is not an efficient method. It creates a new array with the new size then copy old array values to the new array and then replaces the old array with the new array. As you can see this is not a very efficient way of appending an item.

Array.Resize 不是一种有效的方法。它创建一个具有新大小的新数组,然后将旧数组值复制到新数组,然后用新数组替换旧数组。如您所见,这不是一种非常有效的附加项目的方式。

Unless it is a requirement that you have no control on use a List instead of array or create an array with maximum expected dimension from the beginning.

除非要求您无法控制使用 List 而不是数组,或者从一开始就创建一个具有最大预期维度的数组。

For more explanation consider this example: You created an array that can hold million items and fill it with objects. You want to add the million and one object to the array. You use Array.Resize method to change array size to 1000001. When the method tries to create a new array of 1000001 items and copy the items from old array to new array you may get an out of memory error because you are trying to have TWO arrays of a million (holding 2 millions objects) in memory at the same time while all the items that you need are one million +1. Although consider the time and resources lost in creating, copying and replacing.

有关更多解释,请考虑以下示例:您创建了一个可以容纳数百万个项目并用对象填充它的数组。您想将一百万零一个对象添加到数组中。您使用 Array.Resize 方法将数组大小更改为 1000001。当该方法尝试创建一个包含 1000001 个项目的新数组并将这些项目从旧数组复制到新数组时,您可能会遇到内存不足错误,因为您试图拥有两个内存中同时有 100 万个(持有 200 万个对象)的数组,而您需要的所有项目都是 100 万 +1。尽管考虑在创建、复制和替换过程中损失的时间和资源。