用五种语言写数组样例

用五种语言写数组样例

1.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;
}

2.java 的数组

public class Array {
public static void main(String args[]) {
int data[] = new int[3];
data[1] = 20;
data[2] = 30;
for(int x = 0; x < data.length; x++) {
System.out.println(data[x]);
}
}
}

3.C++ 的数组

#include
using namespace std;

int main ()
{
// 一个带有 5 行 2 列的数组
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

// 输出数组中每个元素的值
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{
cout << “a[” << i << “][” << j << "]: ";
cout << a[i][j]<< endl;
}

return 0;
}

4.Basic 语言的数组

Module SimpleArray
Public Sub Main()
’ Declare an array with 7 elements.
Dim students(6) As Integer

  ' Assign values to each element.
  students(0) = 23
  students(1) = 19
  students(2) = 21
  students(3) = 17
  students(4) = 19
  students(5) = 20
  students(6) = 22
  
  ' Display the value of each element.
  For ctr As Integer = 0 To 6
     Dim grade As String = If(ctr = 0, "kindergarten", $"grade {ctr}")
     Console.WriteLine($"Students in {grade}: {students(ctr)}")
  Next

End Sub
End Module
’ The example displays the following output:
’ Students in kindergarten: 23
’ Students in grade 1: 19
’ Students in grade 2: 21
’ Students in grade 3: 17
’ Students in grade 4: 19
’ Students in grade 5: 20
’ Students in grade 6: 22

5.python 语言的数组

name=[[‘Ava’,98],[‘Blake’,85],[‘Connor’,62],[‘David’,75],[‘Emma’,23],[‘Frank’,66]]
6.js 语言的数组
let arr = [1, 2, 3, 4, 5]
let newArr = arr.map(x => x*2)
//arr= [1, 2, 3, 4, 5] 原数组保持不变
//newArr = [2, 4, 6, 8, 10] 返回新数组

数组,变量在不同的语言中意义应该是一样的,语法不相同、定义时的初始值也不太相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值