Silk Array

Array is a kind of data structure that can store a fixed-size sequential collection of elements, The element can be accessed with index, the index begins from 0.
Array is created using square brackets:
array=[];
array=[10,20,30];

An element is accessed by placing the index of the element within square brackets after the name of the array:
print(array[1]);//get the second element,it's 20

The elements in array can be of any data type:
array=[1,true,"good",null];
for(i=0;i<array.size();i++)
    print(array[i]);

Array or other object such as Dictionary/Class in Array is also allowed:
dict1={"name":"wang","age":10};
array1=["good","OK"];
array2=[1,true,"good",null,array1,dict1];
for(i=0;i<array2.size();i++)
    print(array2[i]);

A three-dimensional (3D) array is an array of arrays of arrays:
array1=["good","OK"];
array2=[1,true,"good",null,array1];
array3=[2,array1,array2,"hi"];
print(array3[2][4][0]);

You can declare array/dictionary in array directly:
array2=[1,true,"good",null,["good","OK"],{"wang":30}];
for(i=0;i<array2.size();i++)
    print(array2[i]);

Initialize a two-dimensional array:
arr=[[1,2,3],[4,5,6]];//create a 2*3 two-dimensional(2D) array
print(arr[1][1]);

You can also create a multi-dimensional array like the following function:
//create N*M two-dimensional array
func create_2d_array(N,M,value=0)
{
    array=[];
    array.resize(N);
    for(i=0;i<N;i++)
    {
        array[i]=[];
        array[i].resize(M,value);
    }
    return array;
}
The easier way to create multi-dimensional array is to use the member functions create2d and create3d directly.

Member functions:

append()
 Add an item(element) to the end of the array:
array=["good","OK"];
array.append("Great");
print(array[2]);
array.append(100.25);
print(array[3]);


size()
 Get the count of the elements in array:
print(array.size());


resize(count, value)
 Resizes the container to contain count elements.
 If the current size is greater than count, the container is reduced to its first count elements.
 If the current size is less than count, additional default-inserted elements are appended.
 The default size is 0 when you create an array without any initial value, to change the size, you need to use resize function:
array=[];
array.resize(3);//the size of array will be 3, and the inital value is null
array[0]=123;
array[1]="OK";
array[2]=32.12;
array2=[];
array2.resize(3,"ok");//the size of array2 will be 3, and the inital value for 3 elements is "ok"


clear()
 clear all the elements of the array, and the array size will be 0
array=["good","OK"];
array.clear();
print(array.size());


erase(pos)
 Removes the element at pos
erase(pos,pos2)
 Removes the elements in the range [pos, pos2),  the element at pos2 will not be included.
array=["good","OK",2,100,"fine"];
array.erase(1,3);
for(i=0;i<array.size();i++)
print(array[i]);


insert(pos,item)
 Insert item at pos
array=["good","OK",2,100,"fine"];
array.insert(1,"new");
for(i=0;i<array.size();i++)
print(array[i]);



sort()
 Sort the elements of the array, if there are different data types, the sort will be based on the type seperately.
array=[23,44,24,221,32,4,56,63,3,3,45];
array.sort();
for(i=0;i<array.size();i++)
    print(array[i]);

sort result:
3,3,4,23,24,32,44,45,56,63,221

array=[23,44,24,221,32,4,56,63,3,23.43,4.6,"qabc","bbc"];
array.sort();
for(i=0;i<array.size();i++)
    print(array[i]);

sort result:
bbc,qabc,3,4,23,24,32,44,56,63,221,4.6,23.43


swap(array)
 Exchange the content with another array:
main()
{
    a=[0,1,2];
    b=["a","b"];
    a.swap(b);
    print(a,b);
}
Result:
["a" , "b" ] [0 , 1 , 2 ]


create2d(n,m,val)create3d(n,m,k,val)
 create2d will create an n*m two-dimensional array on itself, the initial value is filled with val, val is optional.
 create3d will create an n*m*k three-dimensional array on itself, the initial value is filled with val, val is optional.
 
main()
{
    a=[];
    a.create2d(2,3,"ok");//create 2*3 array on a, the inital value is "ok"
    print(a);

    b=[];
    b.create3d(2,3,2,0);//create 2*3*2 array on b, the inital value is 0
    print(b);
}
Result:
[["ok" , "ok" , "ok" ] , ["ok" , "ok" , "ok" ] ]
[[[0 , 0 ] , [0 , 0 ] , [0 , 0 ] ] , [[0 , 0 ] , [0 , 0 ] , [0 , 0 ] ] ]


_getptr()_restore(ptr)
_getptr() and _restore(ptr) are used to work around circular reference, please click Memory Management for details.