用五种语言写数组样例
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] 返回新数组