c中如何給陣列新增新的元素,c 中如何給一個陣列新增一個新的元素

2021-07-01 02:06:51 字數 1255 閱讀 1428

1樓:匿名使用者

將陣列轉換成list後,利用list.insert實現插入新元素。方法如下:

/// 指定陣列

/// 待插入的元素

/// 插入的位置

/// 插入後的陣列

static int insertnumber(int a, int value, int index)

catch (exception e)  // 捕獲由插入位置非法而導致的異常

}///

/// 列印陣列

///static void printarray(int a)", x);

}console.writeline();}}}

2樓:匿名使用者

陣列確實不能更改長度。不過你可以用array.copyto方法來將其copy到一個新陣列中。

int newarray = new int[4];

oldarray.copyto( newarray , 0 );

但是你別老是調整,這是有效能問題的。如樓上幾位所說的。你用arraylist或者範型類list就可以解決長度不定的情況。

3樓:草薙在

陣列是沒法改變長度的,如果你需要使用動態陣列請使用arraylist類

4樓:匿名使用者

先遍歷a陣列取出值,然後根據值重新構造一個陣列,包括你要新增的那個3,然後把這個新陣列重新賦值給陣列a!

5樓:匿名使用者

這個有難度,你之前定義的陣列長度為3,a[3]是不可能有的,除非你重新定義一個陣列,建議你用泛型

lista = new list();

……a.add(4);

6樓:匿名使用者

方法一,使用arraylist

arraylist.add(新元素);

方法二,使用為定長陣列

int a={};//其實是很長的陣列

a[0]=1;

a[1]=2;

a[2]=3;

a[3]=4;

方法三,泛型

listl=new list();

l.add(3);

7樓:

listtemarray = new list();

temarray.add(4);

int a =a.toarray();

只能這樣,陣列是不能動態分配記憶體空間的!

C語言題目在陣列中查詢指定元素

include define maxn 10 int search int list,int n,int x int main int search int list,int n,int x return 1 c語言程式 include define n 11 int search int list...

C如何提取陣列中的每字串,C 如何提取陣列中的每一個字串

streamreader sr new streamreader e development hurtcard hurtcard txt 單位配置復.txt encoding.default string line while line sr.readline null 消除空行制 你試試 這個方法...

C語言問題把陣列中的重複元素去掉

define crt secure no deprecate include include include using namespace std 去掉字串中重複的字元並排序 int main int newsize input.size count string str newsize,0 建立...