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

时间:2022-01-15 07:19:50

How do I add a new item from a TextBox and Button on a Windows Form at the end of an ArrayList which references a class?

如何在引用类的ArrayList末尾的Windows窗体上的TextBox和Button中添加新项?

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
  • 在textbox1,textbox2,textbox3中输入新产品详细信息
  • When I click Add the 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)

    值[1] =新产品(“一”,5)值[2] =新产品(“二”,3)值[3] =新产品(“三”,8)值[4] =新产品( “四”,2)

What is the code for doing this?

这样做的代码是什么?

6 个解决方案

#1


28  

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

#2


16  

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

使用List作为其他人提到的。如果您在阵列上设置,请使用

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

#3


3  

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

我认为您需要一个List 集合,查看您的代码。然后只需调用Add()方法即可

#4


2  

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 Resize as 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] = ...

#5


1  

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.

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

#6


0  

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

您可能还应该看一下Array与List:何时使用哪个?

#1


28  

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

#2


16  

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

使用List作为其他人提到的。如果您在阵列上设置,请使用

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

#3


3  

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

我认为您需要一个List 集合,查看您的代码。然后只需调用Add()方法即可

#4


2  

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 Resize as 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] = ...

#5


1  

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.

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

#6


0  

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

您可能还应该看一下Array与List:何时使用哪个?