In Boo, arrays are defined a little differently from the way they are defined in other languages. Here, we create an array of integers and an array of strings: ints = array(int,4) ints[0] = 3 ints[1] = 6 ints[2] = 8 ints[3] = 90 strings = array(string, ["one", "two", "three"]) Note how the type of the array's elements is passed, followed by either the size of the array or the array's elements. One very useful feature of arrays is slicing, which Python users will recognize. Slicing provides an easy way to extract a portion of an array: 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 has support for two built-in collections, lists (for which slicing also works) and hashtables: 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" However, the two collections are not type-safe: c = [1,2] c.Add("three") d = {1: "one", "two": 2} Fortunately, Boo has recently added support for generics, so one can create a type-safe collection using the collections in 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])
blog comments powered by Disqus |
|
|
|
|
|
|
|