Function:
1. Increase, delete, change, check
2. Capacity expansion and reduction
3. Analysis of complexity
4. Averaging complexity
5. Complexity Shock
Analyze the time complexity of a dynamic array:
Analyze the time complexity of resize:
Public classArray<e> { Privatee[] data; Private intsize; //constructor, the capacity of the incoming array capacity constructs an array PublicArray (intcapacity) {Data= (e[])NewObject[capacity]; Size= 0; } //parameterless constructor, default array capacity capacity=10 PublicArray () { This(10); } //get the capacity of an array Public intgetcapacity () {returndata.length; } //gets the number of elements in the array Public intGetSize () {returnsize; } //returns whether the array is empty Public BooleanIsEmpty () {returnSize = = 0; } //inserts a new element at the index index e Public voidAddintindex, E E) { if(Index < 0 | | index >size)Throw NewIllegalArgumentException ("Add failed. Require index >= 0 and index <= size. "); if(Size = =data.length) Resize (2 *data.length); for(inti = size-1; I >= index; I--) Data[i+ 1] =Data[i]; Data[index]=e; Size++; } //add a new element to all elements Public voidAddLast (e e) {Add (size, E); } //add a new element before all elements Public voidAddFirst (e e) {Add (0, E); } //gets the element of the index position PublicE Get (intindex) { if(Index < 0 | | | Index >=size)Throw NewIllegalArgumentException ("Get failed. Index is illegal. "); returnData[index]; } //the element that modifies the index position of index is e Public voidSetintindex, E E) { if(Index < 0 | | | Index >=size)Throw NewIllegalArgumentException ("Set failed. Index is illegal. "); Data[index]=e; } //finds if there are elements in an array e Public Booleancontains (e e) { for(inti = 0; i < size; i + +){ if(Data[i].equals (e))return true; } return false; } //finds the index of the element e in the array and returns 1 if no element e exists Public intFind (e e) { for(inti = 0; i < size; i + +){ if(Data[i].equals (e))returni; } return-1; } //removes the element from the index position from the array, returning the deleted element PublicE Remove (intindex) { if(Index < 0 | | | Index >=size)Throw NewIllegalArgumentException ("Remove failed. Index is illegal. "); E ret=Data[index]; for(inti = index + 1; i < size; i + +) Data[i-1] =Data[i]; Size--; Data[size]=NULL;//loitering Objects! = Memory Leak if(size = = DATA.LENGTH/4 && DATA.LENGTH/2! = 0) Resize (data.length/2); returnret; } //removes the first element from the array, returning the deleted element PublicE Removefirst () {returnRemove (0); } //removes the last element from the array, returning the deleted element PublicE Removelast () {returnRemove (size-1); } //remove an element from an array e Public voidremoveelement (e e) {intindex =find (e); if(Index! =-1) remove (index); } @Override PublicString toString () {StringBuilder res=NewStringBuilder (); Res.append (String.Format ("Array:size =%d, capacity =%d\n", size, data.length)); Res.append (‘[‘); for(inti = 0; i < size; i + +) {res.append (data[i]); if(I! = size-1) Res.append (", "); } res.append (‘]‘); returnres.tostring (); } //Change the capacity of the array space to newcapacity size Private voidResizeintnewcapacity) {e[] NewData= (e[])NewObject[newcapacity]; for(inti = 0; i < size; i + +) Newdata[i]=Data[i]; Data=NewData; }}
Encapsulates a dynamic array class array