Open Source Web Development Tutorials - Dev Shed
.NET言語、Boo入門
(2008/12/02公開)
配列とコレクション
Booでは、配列を定義する方法がほかの言語と少し違います。次の例では、整数の配列と文字列の配列を作成しています。
ints = array(int,4)
ints[0] = 3
ints[1] = 6
ints[2] = 8
ints[3] = 90
strings = array(string, ["one", "two", "three"])
配列の要素の型が渡されてから、次に配列のサイズまたは配列の要素が渡されることに注意してください。
配列の非常に便利な機能の1つに、スライスがあります。Pythonユーザーはこの機能をすぐに理解できるでしょう。スライスを使うと、配列の一部を簡単に抽出できます。
theArray = [1, 2, 3, 4, 5]
print theArray[1:]
print theArray[1:3]
print theArray[2:]
[2, 3, 4, 5]
[2, 3]
[3, 4, 5]
Booでは、リスト(この形式にもスライスが使えます)とハッシュテーブルという2つの組み込みコレクションがサポートされています。
a = [4, 8, 15, 16, 23, 42]
a[0] = 8
a[1] = 15
a.Add(16)
b = {"jdoe": "John Doe", "jsmith": "John Smith"}
b["msmith"] = "Mary Smith"
ただし、これら2つのコレクションは型が決められているわけではありません。
c = [1,2]
c.Add("three")
d = {1: "one", "two": 2}
幸い、Booにはgenericのサポートが最近追加されました。System.Collections.Genericのコレクションを使用して、型が決められたコレクションを作成できます。
import System.Collections.Generic
names = List of string()
names.Add("John")
names.Add("Sam")
names.Add("Mary")
rooms = Dictionary[of int,string]()
rooms.Add(100, names[0])
rooms.Add(200, names[1])
rooms.Add(300, names[2])
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








