动态数组如何工作?
当我们尝试插入时,动态数组(C++ 中的向量, Java中的 ArrayList)会自动增长,并且没有更多空间留给新项目。通常该区域的大小翻倍。
一个简单的动态数组可以通过分配一个固定大小的数组来构造,通常比立即需要的元素数量大。动态数组的元素连续存储在底层数组的开头,而底层数组末尾的剩余位置被保留或未使用。通过使用保留空间,可以在恒定时间内将元素添加到动态数组的末尾,直到该空间完全耗尽。
当所有空间都用完,并且要添加一个额外的元素时,需要增加底层的固定大小数组的大小。通常调整大小是昂贵的,因为您必须分配一个更大的数组并复制您已经过度增长的数组中的所有元素,然后我们才能最终附加我们的项目。
方法:当我们在数组中输入一个元素但数组已满时,您创建一个函数,该函数创建一个新的数组双倍大小或您希望并将所有元素从前一个数组复制到一个新数组并返回这个新数组。此外,我们可以减小数组的大小。并在给定位置添加一个元素,在最后默认和该位置删除元素。
动态阵列的主要特点
添加元素:如果数组大小不够,则在末尾添加元素,然后扩展数组的大小并在原始数组的末尾添加一个元素以及给定的索引。完成所有复制需要 O(n) 时间,其中 n 是我们数组中的元素数。这是一个昂贵的附加成本。在固定长度的数组中,追加只需要 O(1) 时间。
但是只有当我们插入一个完整的数组时,追加才会花费 O(n) 时间。这是非常罕见的,特别是如果我们每次用完空间时都将数组的大小加倍。所以在大多数情况下追加仍然是 O(1) 时间,有时是 O(n) 时间。
在动态数组中,您可以在需要时创建固定大小的数组,然后在数组中添加更多元素,然后使用以下方法:
删除元素:从数组中删除一个元素,默认 remove() 方法从末尾删除一个元素,只需在最后一个索引处存储零,您还可以通过调用 removeAt(i) 方法删除特定索引处的元素,其中 i 是索引。 removeAt(i) 方法将左侧的所有右侧元素从给定索引中移出。
数组大小的调整:当数组右侧有空/零数据(不包括你添加的)时,数组的右侧占用了无用的内存,方法 srinkSize() 释放额外的内存。当所有空间都用完,并且要添加一个额外的元素时,底层的固定大小数组需要增加大小。通常调整大小是昂贵的,因为您必须分配一个更大的数组并复制您已经过度增长的数组中的所有元素,然后我们才能最终附加我们的项目。
动态数组的简单代码。在下面的代码中,数组将变满我们将所有元素复制到新的双倍大小数组(可变大小数组)。示例代码如下
Java
// Java program deals with all operation of a dynamic array
// add, remove, resize memory of array is the main feature
public class DynamicArray {
// create three variable array[] is a array,
// count will deal with no of element add by you and
// size will with size of array[]
private int array[];
private int count;
private int size;
// constructor initialize value to variable
public DynamicArray()
{
array = new int[1];
count = 0;
size = 1;
}
// function add an element at the end of array
public void add(int data)
{
// check no of element is equql to size of array
if (count == size) {
growSize(); // make array size double
} // insert element at end of array
array[count] = data;
count++;
}
// function makes size double of array
public void growSize()
{
int temp[] = null;
if (count == size) {
// temp is a double size array of array
// and store array elements
temp = new int[size * 2];
{
for (int i = 0; i < size; i++) {
// copy all array value into temp
temp[i] = array[i];
}
}
}
// double size array temp initialize
// into variable array again
array = temp;
// and make size is double also of array
size = size * 2;
}
// function shrink size of array
// which block unnecessary remove them
public void shrinkSize()
{
int temp[] = null;
if (count > 0) {
// temp is a count size array
// and store array elements
temp = new int[count];
for (int i = 0; i < count; i++) {
// copy all array value into temp
temp[i] = array[i];
}
size = count;
// count size array temp initialize
// into variable array again
array = temp;
}
}
// function add an element at given index
public void addAt(int index, int data)
{
// if size is not enough make size double
if (count == size) {
growSize();
}
for (int i = count - 1; i >= index; i--) {
// shift all element right
// from given index
array[i + 1] = array[i];
}
// insert data at given index
array[index] = data;
count++;
}
// function remove last element or put
// zero at last index
public void remove()
{
if (count > 0) {
array[count - 1] = 0;
count--;
}
}
// function shift all element of right
// side from given index in left
public void removeAt(int index)
{
if (count > 0) {
for (int i = index; i < count - 1; i++) {
// shift all element of right
// side from given index in left
array[i] = array[i + 1];
}
array[count - 1] = 0;
count--;
}
}
public static void main(String[] args)
{
DynamicArray da = new DynamicArray();
// add 9 elements in array
da.add(1);
da.add(2);
da.add(3);
da.add(4);
da.add(5);
da.add(6);
da.add(7);
da.add(8);
da.add(9);
// print all array elements after add 9 elements
System.out.println("Elements of array:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();
// print size of array and no of element
System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: " +
da.count);
// shrinkSize of array
da.shrinkSize();
// print all array elements
System.out.println("Elements of array "+
"after shrinkSize of array:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();
// print size of array and no of element
System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: " +
da.count);
// add an element at index 1
da.addAt(1, 22);
// print Elements of array after adding an
// element at index 1
System.out.println("Elements of array after" +
" add an element at index 1:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();
// print size of array and no of element
System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: " +
da.count);
// delete last element
da.remove();
// print Elements of array after delete last
// element
System.out.println("Elements of array after" +
" delete last element:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();
// print size of array and no of element
System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: " +
da.count);
// delete element at index 1
da.removeAt(1);
// print Elements of array after delete
// an element index 1
System.out.println("Elements of array after"+
" delete element at index 1:");
for (int i = 0; i < da.size; i++) {
System.out.print(da.array[i] + " ");
}
System.out.println();
// print size of array and no of element
System.out.println("Size of array: " + da.size);
System.out.println("No of elements in array: " +
da.count);
}
}
C#
// C# program deals with all operation
// of dynamic array add, remove, resize
// memory of array is the main feature
using System;
public class DynamicArray
{
// create three variable array[] is
// a array, count will deal with no
// of element add by you and
// size will with size of array[]
private int []array;
private int count;
private int size;
// constructor initialize value to variable
public DynamicArray()
{
array = new int[1];
count = 0;
size = 1;
}
// function add an element at the end of array
public void add(int data)
{
// check no of element is equql to size of array
if (count == size)
{
growSize(); // make array size double
}
// insert element at end of array
array[count] = data;
count++;
}
// function makes size double of array
public void growSize()
{
int []temp = null;
if (count == size)
{
// temp is a double size array of array
// and store array elements
temp = new int[size * 2];
{
for (int i = 0; i < size; i++)
{
// copy all array value into temp
temp[i] = array[i];
}
}
}
// double size array temp initialize
// into variable array again
array = temp;
// and make size is double also of array
size = size * 2;
}
// function shrink size of array
// which block unnecessary remove them
public void shrinkSize()
{
int []temp = null;
if (count > 0)
{
// temp is a count size array
// and store array elements
temp = new int[count];
for (int i = 0; i < count; i++)
{
// copy all array value into temp
temp[i] = array[i];
}
size = count;
// count size array temp initialize
// into variable array again
array = temp;
}
}
// function add an element at given index
public void addAt(int index, int data)
{
// if size is not enough make size double
if (count == size)
{
growSize();
}
for (int i = count - 1; i >= index; i--)
{
// shift all element right
// from given index
array[i + 1] = array[i];
}
// insert data at given index
array[index] = data;
count++;
}
// function remove last element or put
// zero at last index
public void remove()
{
if (count > 0)
{
array[count - 1] = 0;
count--;
}
}
// function shift all element of right
// side from given index in left
public void removeAt(int index)
{
if (count > 0)
{
for (int i = index; i < count - 1; i++)
{
// shift all element of right
// side from given index in left
array[i] = array[i + 1];
}
array[count - 1] = 0;
count--;
}
}
// Driver code
public static void Main()
{
DynamicArray da = new DynamicArray();
// add 9 elements in array
da.add(1);
da.add(2);
da.add(3);
da.add(4);
da.add(5);
da.add(6);
da.add(7);
da.add(8);
da.add(9);
// print all array elements after add 9 elements
Console.WriteLine("Elements of array:");
for (int i = 0; i < da.size; i++)
{
Console.Write(da.array[i] + " ");
}
Console.WriteLine();
// print size of array and no of element
Console.WriteLine("Size of array: " + da.size);
Console.WriteLine("No of elements in array: " +
da.count);
// shrinkSize of array
da.shrinkSize();
// print all array elements
Console.WriteLine("Elements of array "+
"after shrinkSize of array:");
for (int i = 0; i < da.size; i++)
{
Console.Write(da.array[i] + " ");
}
Console.WriteLine();
// print size of array and no of element
Console.WriteLine("Size of array: " + da.size);
Console.WriteLine("No of elements in array: " +
da.count);
// add an element at index 1
da.addAt(1, 22);
// print Elements of array after adding an
// element at index 1
Console.WriteLine("Elements of array after" +
" add an element at index 1:");
for (int i = 0; i < da.size; i++)
{
Console.Write(da.array[i] + " ");
}
Console.WriteLine();
// print size of array and no of element
Console.WriteLine("Size of array: " + da.size);
Console.WriteLine("No of elements in array: " +
da.count);
// delete last element
da.remove();
// print Elements of array after delete last
// element
Console.WriteLine("Elements of array after" +
" delete last element:");
for (int i = 0; i < da.size; i++)
{
Console.Write(da.array[i] + " ");
}
Console.WriteLine();
// print size of array and no of element
Console.WriteLine("Size of array: " + da.size);
Console.WriteLine("No of elements in array: " +
da.count);
// delete element at index 1
da.removeAt(1);
// print Elements of array after delete
// an element index 1
Console.WriteLine("Elements of array after"+
" delete element at index 1:");
for (int i = 0; i < da.size; i++)
{
Console.Write(da.array[i] + " ");
}
Console.WriteLine();
// print size of array and no of element
Console.WriteLine("Size of array: " + da.size);
Console.WriteLine("No of elements in array: " +
da.count);
}
}
/* This code contributed by PrinciRaj1992 */
Elements of array:
1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0
Size of array: 16
No of elements in array: 9
Elements of array after shrinkSize of array:
1 2 3 4 5 6 7 8 9
Size of array: 9
No of elements in array: 9
Elements of array after add an element at index 1:
1 22 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0
Size of array: 18
No of elements in array: 10
Elements of array after delete last element:
1 22 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0
Size of array: 18
No of elements in array: 9
Elements of array after delete element at index 1:
1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0
Size of array: 18
No of elements in array: 8