Part 05 - Containers and Casting
Lists
| Definition: List A linked list that can hold a variable amount of |
Lists are mutable, which means that the List can be changed, as well as its children.
[0, alpha, 4.5, d] [a, b, c, d, e, f, g, h, i, j] [0, 1, 2, 3, 4] [0, 1, 5, 3, 4] [0, 1, 5, 'banana', 4] [0, 1, 5, 'banana', 4, 100.1] [0, 5, 'banana', 4, 100.1] 0 5 'banana' 4 100.1
As you can see, Lists are very flexible, which is very handy.
Lists can be defined two ways:
- by using brackets
[] - by creating a new
Listwrapping anIEnumerator, or anarray.
Slicing
Slicing is quite simple, and can be done to strings, Lists, and arrays.
It goes in the form var[start:end]. both start and end are optional, and must be integers, even negative integers.
To just get one child, use the form var[position]. It will return a char for a string, an object for a List, or the specified type for an array.
Slicing counts up from the number 0, so 0 would be the 1st value, 1 would be the 2nd, and so on.
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4] [2, 3, 4] [5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7] [6, 7] 5 2 --- abcdefghij abcde cde fghij abcdefgh gh f c
I hope you get the idea. Slicing is very powerful, as it allows you to express what you need in a minimal amount of space, while still being readable.
Arrays
| Definition: Array Arrays are simple objects that hold equally-sized data elements, generally of the same data type. |
Arrays, unlike Lists, cannot change their size. They can still be sliced, just not added on to.
Arrays can be defined three ways:
- by using parentheses
()- If you have 0 members, it's declared:
(,) - If you have 1 member, it's declared:
(member,) - If you have 2 or more members, it's declared:
(one, two)
- If you have 0 members, it's declared:
- by creating a new
arraywrapping anIEnumerator, or anList. - by creating a blank
arraywith a specified size:array(type, size)
(0, alpha, 4.5, d) (a, b, c, d, e, f, g, h, i, j) (0, 1, 2, 3, 4) (0, 1, 5, 3, 4) ERROR: Cannot convert 'System.String' to 'System.Int32'.
Arrays, unlike Lists, do not necessarily group objects. They can group any type, in the case of array(range(5)), it made an array of ints.
List to Array Conversion
If you create a List of ints and want to turn it into an array, you have to explicitly state that the List contains ints.
[0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] (0, 1, 2, 3, 4) (0, 1, 7, 3, 4) ERROR: Operator '+' cannot be used with a left-hand side of type 'System.Object' and a right-hand side of type 'System.Int32'
This didn't work, because the List still gives out objects instead of ints, even though it only holds ints.
Casting
| Definition: Typecast The conversion of a variable's data type to another data type to bypass some restrictions imposed on datatypes. |
To get around a list storing only objects, you can cast an object individually to what its type really is, then play with it like it should be.
Granted, if you cast to something that is improper, say a string to an int, Boo will emit an error.
There are two ways to cast an object as another data type.
- using
var as <type> - using
var cast <type>
[0, 1, 2, 3, 4] 0 5 10 15 20 --- 0 1 4 9 16
| Recommendation Try not to cast too much. |
| Upcoming feature: Generics Generics, which will be part of the .NET Framework 2.0, will allow you to create a |
Hashes
| Definition: Hash A |
Hashes are also called "dictionaries" in some other languages.
Hashes are very similar to Lists, except that the key in which to set values can be an object, though usually an int or a string.
Hashes can be defined two common ways:
- by using braces {}
- by creating a new
Hashwrapping anIEnumerator, or anIDictionary.
1 the answer --- a => 1 b => 2 monkey => 3 42 => the answer
Exercises
- Produce a
Listcontaining the fibonacci sequence that has 1000 values in it. (See if you can do it in 4 lines)
Go on to Part 06 - Operators
