如何动态地将值添加到数组中

如何动态地将值添加到数组中

问题描述:

如何动态地将值添加到数组中





你能给我提供代码吗?

how to add values into array dynamically


could you please provide me code?

我假设你的意图是通过一个或多个索引来增长数组 - 这是不可能的。您必须分配具有所需大小的新数组,并将旧数组的值复制到其中。参见 Array.Copy(..) [ ^ ]



也许你应该考虑使用一个标准的Collection-Classes,比如列表< t> [ ^ ]或适合您的任何内容。这些将自动处理底层存储的增长。
I assume your intention is to grow an array by one or more indices - that's not possible. You would have to allocate a new array that has the desired size and copy the values of the old array into it. See Array.Copy(..)[^]

Maybe you should consider using one of the standard Collection-Classes instead, like List<t>[^] or whatever would suit you. Those would handle growing the underlying storage automatically.