如何往数组里添加值

[复制链接]
查看11 | 回复3 | 2020-1-20 08:51:01 | 显示全部楼层 |阅读模式
数组是不允许再往里面添加值的,你可以将数组放到List里面如
int[] a ={ x, x, x };
List list = new List();
foreach (int i in a)
{
list.Add(i);
}
list.Add(y);
回复

使用道具 举报

千问 | 2020-1-20 08:51:01 | 显示全部楼层
直接用动态数组嘛ArrayList al=new ArrayList();al.Add(y);
回复

使用道具 举报

千问 | 2020-1-20 08:51:01 | 显示全部楼层
a[0] = 0;a[1] = 1;...
回复

使用道具 举报

千问 | 2020-1-20 08:51:01 | 显示全部楼层
不要用数组,用List吧List a=new List(new int[]{x,x,x});a.Add(b); 那写个函数吧private int[] add(int[] array, int j){
int[] temp = array;
array = new int[temp.Length + 1];
for (int i = 0; i < temp.Length; i++)
{
array = temp;
}
array[array.Length - 1] = j;
return array;}然后调用它,比如你要改的是array数组,要往里加个10array=add(array,10);使用动态数组
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行