Array 数组
Array
CBrother数组是任何对象的有序整数索引集合,数组的索引从 0 开始,CBrother数组不需要指定大小,当向数组添加元素时,CBrother数组会自动增长。
例子:
function main(parm)
{
var myarray = new Array(1,2);
myarray.add("i'am string");
var len = myarray.size();
print "myarray size:" + len;
print "myarray 0:" + myarray[0];
print "myarray 1:" + myarray[1];
print "myarray 2:" + myarray[2];
myarray.remove(1);
len = myarray.size();
print "after remove. myarray size:" + len;
print "myarray 0:" + myarray[0];
print "myarray 1:" + myarray[1];
myarray.insert(0,55.55);
myarray[1] = 100;
len = myarray.size();
print "after insert. myarray size:" + len;
print "myarray 0:" + myarray[0];
print "myarray 1:" + myarray[1];
print "myarray 2:" + myarray[2];
myarray.clear();
len = myarray.size();
print "after clear. myarray size:" + len;
}
结果:
myarray size:3
myarray 0:1
myarray 1:2
myarray 2:i'am string
after remove. myarray size:2
myarray 0:1
myarray 1:i'am string
after insert. myarray size:3
myarray 0:55.55000
myarray 1:100
myarray 2:i'am string
after clear. myarray size:0
Array也可以只直接用[]来初始化:
function main(parm)
{
var myarray = [1,2,"i'am string"];
var len = myarray.size();
print "myarray size:" + len;
print "myarray 0:" + myarray[0];
print "myarray 1:" + myarray[1];
print "myarray 2:" + myarray[2];
}
结果:
myarray size:3
myarray 0:1
myarray 1:2
myarray 2:i'am string
foreach支持
foreach语句中的key表示元素在Array中的索引,value表示这个元素
function main(parm)
{
var array = [1,2,3];
print "foreach:";
foreach(k,v : array)
{
print "k:" + k + ",v=" + v;
}
print "foreach_reverse:";
foreach_reverse(k,v : array)
{
print "k:" + k + ",v=" + v;
}
}
结果:
foreach:
k:0,v=1
k:1,v=2
k:2,v=3
foreach_reverse:
k:2,v=3
k:1,v=2
k:0,v=1