如何在数组中插入元素 [英] How to Insert elements in an array

查看:117
本文介绍了如何在数组中插入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在数组中添加元素。

我可以添加字符串或整数。



但是如何插入元素已经有一些元素的数组

I know how to add elements in array.
I can add either string or integers.

But how to insert elements in an array which already has some elements

推荐答案

检查数组是否是动态数组(这意味着它可以增加大小)如果不是创建数组并传输内容现有数组到新创建的数组
Check if the array is a dynamic Array ( which means it can increase in size ) if not the create one and transfer the content of the existing array to the newly created one


如何制作动态数组?

数组的大小是在编译时分配的,而不是在运行时分配的。

使用 C语言,您可以通过 malloc 功能实现。



所以在C#中,如果要向数组中添加元素,请执行以下操作:

How can you make dynamic array ?
Size of the array is assigned at compile time and not at run time.
With C language you can achieve through malloc function.

So in C#, if you want to add elements to the array, do following,
string [] strArray = new string[5];
List<string> strList = strArray.ToList();
strList.Add("CodeProject");
strArray = strList.ToArray();



我还没有检查它,但它应该有效。如果有任何错误,请告诉我。



-KR


I have not check it yet, but it should work. Let me know if there is any error.

-KR


这篇关于如何在数组中插入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆