python 列表,元素,字典
数组三种模式:列表(list),元组(tuple),字典(dict),均支持遍历,也支持迭代
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| demo1 = ['a','b','c','d'] demo2 = ('a','b','c','d') demo3 = {"name":"kurt","age":18,"sex",1}
for item in demo1: print item
for key in xrange(0, len(demo1)): print key, demo1[key]
for key in demo3: print key
for value in demo3.values(): print value
for item in demo3.items(): print item
rs = demo1.iteritems() print rs.next() print rs.next()
|
php数组
php数组支持一维数组和多维数组的混搭。数组的value可以是任意类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
$arr1 = array( "name"=>"kurt", "age" =>18, "sex" =>1 );
$arr2 = [ 12, 34,2,41,5 ];
$arr3 = [ 'http' => ['nginx','apache','iis'], 'cache' => ['redis','memcache'], 'database' => ['mysql','orecal','DB2','mssql'], 'remark' => '多维数组下面的value是相对独立' ]
foreach ($arr1 as $k=>$v) { echo $k,$v ; }
|
java数组
数组中可以存放任意类型的数据,但是同一个数组存放的元素必须一致。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| # 声明并创建可以存3个整数的数组 int[] arr0 = new int[3] ; # 先声明后创建可以存3个整数数组 int[] arr1; arr1=new int[3];
# 数组赋值。初始化数组的时候会赋值为0. arr0[0] = 'a'; arr0[1] = 'b'; arr0[2] = 'c'; # arr0[3] = 'd'; # 报错,超出索引
# 数组静态初始化。。 int[] arr2 = new int[]{1,2,3,4}; # 声明并初始化并复制 int[] arr3 = {1,2,3,4}; # 声明后直接复制
# for循环遍历数组 for (int i = 0; i <arr0.length ; i++) { System.out.print(arr0[i]); }
|