(Java作业04)5种语言写数组

一、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’];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值