-
Arrays are not allowed to add values to them, you can put them in a list.
As. int a =;
listlist = new list();
foreach (int i in a);;
-
Just use dynamic arrays.
arraylist al=new arraylist();
-
Don't use arrays, use lists.
lista=new list(new int);
Let's write a function.
private int add(int array, int j)int temp = array;
array = new int[ +1];
for (int i = 0; i < i++)array[i] = temp[i];
array[ -1] = j;
return array;
Then call it, for example, if you want to change the array array, you need to add a 10array=add(array,10);
-
There are three ways to add an element to an array
1. Generally, elements cannot be added to arrays, because they have already set the length at the initialization time, and the length cannot be changed.
However, there is an array that can be changed in size as an arraylist, that is, you can define an arraylist array, and then use the add(element) method to add elements to it, and you can also add(index,element) to add elements to the specified subscript.
2. The idea is to convert the array into a list first, add elements with the add() method of the list, and then convert the list into an array.
But there's a blind spot here, when converting an array to a list, the aslist() method used will return a final, fixed-length arraylist class, and no, using it as an add() or remove() class doesn't work.
So what should be done, when defining the list, directly convert the array.
The third method idea is to create a new array, the size of the new array is the size of the old array + 1, copy the elements in the old array into the new array, and add the elements to be added to the new array.
-
1. Use ArrayList to define an array, ArrayList has add, insert methods, you can freely add new data to the array.
2. Add an element to the array, run the array test program, and output the result to the console. Add an array element.
2.The size of the array list is that it actually contains one element:",,
3. If you want to add data to the closed array in batches, you can use addrange to achieve it:
int to add elements in bulk at the end of the arraylist.
4. If you want to add data to the specified position of the array, you can use the insert method to implement it:
88);Add an array element.
5. If you don't use arraylist to define the array, you will find that the array is fixed, so you can manually make the data "longer"; The task of adding data to an array is done with the concat function that concats two arrays, and the toarray function that creates the array function.
6. Pass "x = int toarray(); Add data, run the array test program, and feed the results to the console.