一、C
# include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i=0; i<5; ++i)
{
printf("%d\n", a[i]); //输出数组中的元素的值
}
return 0;
}
**
二、C++
**
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main ()
{
int n[ 10 ]; // n 是一个包含 10 个整数的数组
// 初始化数组元素
for ( int i = 0; i < 10; i++ )
{
n[ i ] = i + 100; // 设置元素 i 为 i + 100
}
cout << "Element" << setw( 13 ) << "Value" << endl;
// 输出数组中每个元素的值
for ( int j = 0; j < 10; j++ )
{
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
}
return 0;
}
**
三、Java
**
创建一个长度为4的数组
//类型[] 数组名=new 类型[长度];
int[] arr = new int[4]; //创建一个长度为4的数组
//类型[] 数组名={数组成员,数组成员,数组成员};
int[] arr={1,2,3,4};
**
四、Python
**
matrix=[0,1,2,3] //直接定义
matrix=[0 for i in range(4)] print(matrix) //间接定义
**
五、PHP
**
//二维数组
$array = [
[‘colorId’ => 1, ‘color’ => ‘red’],
[‘colorId’ => 2, ‘color’ => ‘blue’],
[‘colorId’ => 3, ‘color’ => ‘yellow’],
];
//一维数组
$a = [‘colorId’ => 1, ‘color’ => ‘red’];