Arrays (indexed/associative)
Demonstrates creating and reading indexed arrays and associative arrays (key-value pairs).
C C23
C23
#include <stdio.h>
typedef struct { const char *key; int val; } Pair;
int main(void) {
int nums[] = {10, 20, 30}; // indexed array
Pair scores[] = { {"math", 95}, {"english", 88} }; // assoc array: simulated with a struct
printf("%d\n", nums[0]);
printf("%d\n", scores[0].val);
return 0;
}
Output
10 95
C++ C++26
C++26
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::vector<int> nums = {10, 20, 30}; // indexed array
std::map<std::string, int> scores = { {"math", 95}, {"english", 88} }; // assoc array
std::cout << nums[0] << "\n";
std::cout << scores["math"] << "\n";
return 0;
}
Output
10 95
C# v11.0.100
v11.0.100
var nums = new[] { 10, 20, 30 }; // indexed array
var scores = new Dictionary<string, int> { ["math"] = 95, ["english"] = 88 }; // assoc array
Console.WriteLine(nums[0]);
Console.WriteLine(scores["math"]);
Output
10 95
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
nums := []int{10, 20, 30}
scores := map[string]int{"math": 95, "english": 88}
fmt.Println(nums[0])
fmt.Println(scores["math"])
}
Output
10 95
Java v27.0.1
v27.0.1
import java.util.Map;
public class Main {
public static void main(String[] args) {
int[] nums = {10, 20, 30};
Map<String, Integer> scores = Map.of("math", 95, "english", 88);
System.out.println(nums[0]);
System.out.println(scores.get("math"));
}
}
Output
10 95
JavaScript v26.13.1
v26.13.1
const nums = [10, 20, 30];
const scores = { math: 95, english: 88 };
console.log(nums[0]);
console.log(scores.math);
Output
10 95
PHP v8.6.0
v8.6.0
$nums = [10, 20, 30];
$scores = ["math" => 95, "english" => 88];
echo $nums[0], "\n";
echo $scores["math"];
Output
10 95
Python v3.15.2
v3.15.2
nums = [10, 20, 30]
scores = {"math": 95, "english": 88}
print(nums[0])
print(scores["math"])
Output
10 95
Rust v1.99.1
v1.99.1
use std::collections::HashMap;
fn main() {
let nums = [10, 20, 30];
let scores: HashMap<&str, i32> = HashMap::from([("math", 95), ("english", 88)]);
println!("{}", nums[0]);
println!("{}", scores["math"]);
}
Output
10 95
TypeScript v26.13.1
v26.13.1
const nums: number[] = [10, 20, 30];
const scores: Record<string, number> = { math: 95, english: 88 };
console.log(nums[0]);
console.log(scores.math);
Output
10 95
Language differences
C: models indexed/associative arrays with plain arrays and structs.
Go: indexed arrays are called slices; associative arrays are called map, iterating in no defined order.
Java: associative arrays are called Map; Map.of returns an immutable Map.
JavaScript: indexed arrays are called arrays; associative arrays are called objects.
PHP: uses the single array type for both indexed and associative arrays.
Python: indexed arrays are called list; associative arrays are called dict, preserving insertion order since 3.7.
Rust: indexed arrays are called arrays; associative arrays are called HashMap, iterating in no defined order.