Length (associative array)

From SwiftAPI

Jump to: navigation, search

Contents

C++

map<string, int> a;
a["test"] = 4;
a["foo"] = 2;
int n = a.size();
// n == 2

C#

MSDN

System.Collections.Generic.ICollection<T> : int Count { get; }
using System;
using System.Collections.Generic;
 
Dictionary<string, int> a = new Dictionary<string, int>();
a["test"] = 4;
a["foo"] = 2;
int n = a.Count;
// n == 2

Java

J2SE 5

java.util.Map : public int size()
import java.util.Map;
import java.util.HashMap;
 
Map<String, Integer> a = new HashMap<String, Integer>();
a.put("test", 4);
a.put("foo", 2);
int n = a.size();
// n == 2

JavaScript

var a = {test: 2, foo: 4};
var n = 0;
for(var k in a) {
    n++;
}
// n == 2

Objective-C

developer.apple.com

@property(readonly) NSUInteger count
NSDictionary *a = @{@"test": @4, @"foo": @2};
int n = a.count;
// n == 2

Perl

%a = ();
$a{'test'} = 4;
$a{'foo'} = 2;
$n = keys(%a); # getting the length of the keys list
# $n == 2

PHP

php.net

int count ( mixed $var [, int $mode = COUNT_NORMAL ] )
$a = array();
$a['test'] = 4;
$a['foo'] = 2;
$n = count($a);
// $n == 2

Python

docs.python.org

a = {}
a['test'] = 4
a['foo'] = 2
n = len(a)
# n == 2

Ruby

Ruby-Doc.org

hsh.length => fixnum
a = {}
a["test"] = 4
a["foo"] = 2
n = a.length
# n == 2

Swift

let a = ["test": 4, "foo": 2]
let n = a.count
// n == 2
Personal tools