New dictionary

Jmnote (토론 | 기여)님의 2017년 4월 13일 (목) 10:33 판 (→‎Java)
Associative arrays
Hash
hash map
hash table
Dictionary
dict
NSDictionary
NSMutableDictionary

1 Bash

declare -A MEMBER
MEMBER=(["ID"]=102 ["Name"]="YONEZAWA Akinori" ["Address"]="Naha, Okinawa")

for k in "${!MEMBER[@]}"; do
  echo "[$k] ${MEMBER[$k]}"
done
# [Address] Naha, Okinawa
# [ID] 102
# [Name] YONEZAWA Akinori
declare -A MEMBER
MEMBER["ID"]=102
MEMBER["Name"]="YONEZAWA Akinori"
MEMBER["Address"]="Naha, Okinawa"
declare -A MEMBER
MEMBER=(["ID"]=102)
MEMBER+=(["Name"]="YONEZAWA Akinori")
MEMBER+=(["Address"]="Naha, Okinawa")

2 C#

Hashtable member = new Hashtable();
member.Add("ID", "102");
member.Add("Name", "YONEZAWA Akinori");
member.Add("Address", "Naha, Okinawa");
Dictionary<string, string> member = new Dictionary<string, string>();
member.Add("ID", "102");
member.Add("Name", "YONEZAWA Akinori");
member.Add("Address", "Naha, Okinawa");
Dictionary<int, string> member = new Dictionary<int, string> {
  { "ID", "102" },
  { "Name", "YONEZAWA Akinori" },
  { "Address", "Naha, Okinawa" }
};

3 Java

HashMap<String,Object> member = new HashMap<String,Object>();
member.put("ID", "102");
member.put("Name", "YONEZAWA Akinori");
member.put("Address", "Naha, Okinawa");
System.out.println( member );
// {Address=Naha, Okinawa, ID=102, Name=YONEZAWA Akinori}

4 JavaScript

  • literal notation
var member = {
'ID' : '102',
'Name' : 'YONEZAWA Akinori',
'Address' : 'Naha, Okinawa'
};
console.log(member);
// { ID: '102', Name: 'YONEZAWA Akinori', Address: 'Naha, Okinawa' }
var member = {
ID : '102',
Name : 'YONEZAWA Akinori',
Address : 'Naha, Okinawa'
};
console.log(member);
  • dot notation
var member = {}
member.ID = '102';
member.Name = 'YONEZAWA Akinori';
member.Address = 'Naha, Okinawa';
console.log(member);
var member = new Object();
member.ID = "102";
member.Name = "YONEZAWA Akinori";
member.Address = "Naha, Okinawa";
console.log(member);
  • bracket notation
var member = {}
member['ID'] = "102";
member['Name'] = "YONEZAWA Akinori";
member['Address'] = "Naha, Okinawa";
console.log(member);
var member = new Object();
member['ID'] = "102";
member['Name'] = "YONEZAWA Akinori";
member['Address'] = "Naha, Okinawa";
console.log(member);

5 Objective-C

NSMutableDictionary *member = [[NSMutableDictionary alloc] init];
[member setObject:@"102" forKey:@"ID"];
[member setObject:@"YONEZAWA Akinori" forKey:@"Name"];
[member setObject:@"Naha, Okinawa" forKey:@"Address"];

6 PHP

$member = array(
  "ID" => 102,
  "Name" => "YONEZAWA Akinori",
  "Address"=> "Naha, Okinawa"
);
$member = array();
$member['ID'] = 102;
$member['Name'] = "YONEZAWA Akinori";
$member['Address'] = "Naha, Okinawa";

7 Python

d = dict(a=1, b=2, c=3)
print( d )
# {'b': 2, 'c': 3, 'a': 1}
member = { 'ID' : 102, 'Name' : 'YONEZAWA Akinori', 'Address' : 'Naha, Okinawa' }
from pprint import pprint
member = { }
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
pprint( member )
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}

8 Ruby

  • string key
member = {
  "ID" => 102,
  "Name" => "YONEZAWA Akinori",
  "Address"=> "Naha, Okinawa"
}
puts member 
# {"ID"=>102, "Name"=>"YONEZAWA Akinori", "Address"=>"Naha, Okinawa"}
member = {}
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
puts member
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}
member = Hash.new
member['ID'] = 102
member['Name'] = 'YONEZAWA Akinori'
member['Address'] = 'Naha, Okinawa'
puts member
# {'Address': 'Naha, Okinawa', 'ID': 102, 'Name': 'YONEZAWA Akinori'}
  • symbol key
member = {
  ID: 102,
  Name: "YONEZAWA Akinori",
  Address: "Naha, Okinawa"
}
print member 
# {:ID=>102, :Name=>"YONEZAWA Akinori", :Address=>"Naha, Okinawa"}

9 같이 보기