Several methods of array resizing and array resizing
Suppose there is an array with a specified length. How can we resize it? The easiest way to scale up is as follows:
class Program
{
static void Main(string[] args)
{
int[] arrs = new[] {1, 2, 3, 4, 5};
arrs[5] = 6;
}
}
Error: IndexOutOfRanageException is not processed, and the index exceeds the array limit.
□Create a temporary expanded array and assign it to the original array.
static void Main(string[] args)
{
int[] arrs = new[] {1, 2, 3, 4, 5};
int[] temp = new int[arrs.Length + 1];
// Traverse the arrs array and assign all elements of the array to the temp Array
for (int i = 0; i < arrs.Length; i++)
{
temp[i] = arrs[i];
}
// Assign a temporary array to the original array, and the original array has been expanded.
arrs = temp;
// Assign a value to the last position of the original array after expansion
arrs[arrs.Length - 1] = 6;
foreach (var item in arrs)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
□Create a temporary Array for expansion, assign a value to the original Array, and use the static method of Array
For normal copying between arrays, the Array class must have prepared a static method: Array. Copy ().
static void Main(string[] args)
{
int[] arrs = new[] {1, 2, 3, 4, 5};
int[] temp = new int[arrs.Length + 1];
Array.Copy(arrs, temp, arrs.Length);
// Assign a temporary array to the original array, and the original array has been expanded.
arrs = temp;
// Assign a value to the last position of the original array after expansion
arrs[arrs.Length - 1] = 6;
foreach (var item in arrs)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
□Use the static method of Array to resize
However, copying and copying are cumbersome. We can also use the Array. Resize () method to Resize the Array.
static void Main(string[] args)
{
int[] arrs = new[] {1, 2, 3, 4, 5};
Array.Resize(ref arrs, arrs.Length + 1);
// Assign a value to the last position of the original array after expansion
arrs[arrs.Length - 1] = 6;
foreach (var item in arrs)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
Summary: Array expansion gives priority to the use of the static method of Array Resize, and secondly, to assign a temporary and expanded Array to the original Array.
How to expand space using dynamic arrays in C Language
Then, I will change the previous answer. You mean to build a memory management stack system.
You can consider doing so.
Int * p = NULL;
P = malloc (xxx * sizeof (int); // xxx your bucket must have a size.
Int * pFirst = p;
Int size = 0;
Then encapsulate an int * my_malloc (malloc_size)
{
Int ret_val = size;
Size + = malloc_size;
Return p [size];
}
This function records the first address of the returned space.
Call my_malloc every time you input a data record and add the size of your input data.
Which of the following definition methods are available for js arrays?
Js variable definition is flexible
Var arr = ();
Var arr = [];
Var arr = new Array ();
These are all possible.