循环关联数组并打印项目

Contents

输出结果

January
February
March

程式码


C

#include <stdio.h>
#include <stdlib.h>
 
typedef struct {
    int size;
    void **keys;
    void **values;
} hash_t;
 
hash_t *hash_new (int size) {
    hash_t *h = calloc(1, sizeof (hash_t));
    h->keys = calloc(size, sizeof (void *));
    h->values = calloc(size, sizeof (void *));
    h->size = size;
    return h;
}
 
int hash_index (hash_t *h, void *key) {
    int i = (int) key % h->size;
    while (h->keys[i] && h->keys[i] != key)
        i = (i + 1) % h->size;
    return i;
}
 
void hash_insert (hash_t *h, void *key, void *value) {
    int i = hash_index(h, key);
    h->keys[i] = key;
    h->values[i] = value;
}
 
void *hash_lookup (hash_t *h, void *key) {
    int i = hash_index(h, key);
    return h->values[i];
}
 
int main () {
    hash_t *h = hash_new(15);
    hash_insert(h, "1", "January");
    hash_insert(h, "2", "February");
    hash_insert(h, "3", "March");
    printf("%s\n", hash_lookup(h, "1"));
    printf("%s\n", hash_lookup(h, "2"));
    printf("%s\n", hash_lookup(h, "3"));
    return 0;
}

C++

#include <iostream>
#include <map>
using namespace std;
int main() {
    map<string, string> monArray;
    monArray["1"] = "January";
    monArray["2"] = "February";
    monArray["3"] = "March";
    for (map<string, string>::iterator p = monArray.begin(); p != monArray.end(); ++p ) {
         cout << p->second << endl;
   }
}

C#

AssociativeArray.cs

using System;
using System.Collections.Generic;

public class AssociativeArray {
    public void printArray() {
        Dictionary<string, string>
        array = new Dictionary<string, string>();
        array["1"] = "January";
        array["2"] = "February";
        array["3"] = "March";
        System.Console.Write("{0}\n", array["1"]);
        System.Console.Write("{0}\n", array["2"]);
        System.Console.Write("{0}\n", array["3"]);
    }
    public static void Main()  {
         AssociativeArray aa = new AssociativeArray();
         aa.printArray();
    }
}

Go

package main
import "fmt"
func main() {
	array := map[string] string{"1": "January", "2":"February", "3":"March"}
	fmt.Println(array["1"])
	fmt.Println(array["2"])
	fmt.Println(array["3"])	
}

Java

AssociativeArray.java

import java.util.*;
public class AssociativeArray {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "January");
        map.put("2", "February");
        map.put("3", "March");
        System.out.println(map.get("1"));
        System.out.println(map.get("2"));
        System.out.println(map.get("3"));
    }
}

JavaScript

let array = {1: 'January', 2:'February', 3:'March'};
console.log(array[1]);
console.log(array[2]);
console.log(array[3]);

Perl

my $array = {"1" => 'January', "2" => 'February', "3" => 'March'};
print($array->{"1"}."\n");
print($array->{"2"}."\n");
print($array->{"3"}."\n");

PHP

<?php
$array = ["1" => "January", "2" => "February", "3" => "March"];
print($array["1"]."\n");
print($array["2"]."\n");
print($array["3"]."\n");

Python

array = {"1": "January", "2":"February", "3":"March"}
print(array["1"])
print(array["2"])
print(array["3"])

Ruby

array = {"1" => "January", "2" => "February", "3" => "March"}
print(array["1"]+"\n")
print(array["2"]+"\n")
print(array["3"]+"\n")

Shell

#!/bin/sh
ARRAY=(
  ["1"]="January"
  ["2"]="February"
  ["3"]="March"
)

echo ${ARRAY["1"]}
echo ${ARRAY["2"]}
echo ${ARRAY["3"]}