Simple Collection Examples
Unsorted and Non-Unique
With the following code you can create an unsorted, non-unique Collection and print out the contents:
- The index is 0-based and a Long. This is to accommodate Collections that are more than 32,767 elements.
On line 1 you instantiate a Collection that can contain any scalar value and is unsorted and can contain duplicate elements. On lines 5 to 10, you add values individually. You then populate a double array on lines 12 to 15, and use addAll()
to add them in a single call into the Collection.
You can use a Do...Loop While
to iterate through the Collection and print out the values.
i
is incremented at the end of the loop, before being compared to coll.ElementCount
. Because the Collection is zero-indexed, ElementCount
is one higher, so you check whether the next number you will iterate is greater than ElementCount. They're printed according to insertion order.
But you can insert an element at a particular index.
The printed out content will now output "Hello,New,World,1,2,3,Hello,1.23,21.648,8472.6,746", with the word "New" inserted as the second element.
Sort and Unique
With the following code you can create a unique and sorted Collection:
Dim coll2 as New Collection("SCALAR", Nothing, True, True)
Call coll2.add("Hello")
Call coll2.add("World")
Call coll2.add(1)
Call coll2.add(2)
Call coll2.add(3)
Call coll2.add(2.5)
Call coll2.add("Hello")
When you iterate this Collection, the numeric values will be inserted at the start of the Collection and the strings at the end. Integers and doubles will be treated as the same data-type, so 2.5 will appear between 2 and 3. The duplicate "Hello" will be ignored. When you iterate the Collection to print the values out, you will get:
Note
insertAt()
will generate an error if you try to use it, because you have specfied that the Collection should be sorted.
Reversing and Adding
You can reverse the Collection and adding will still add elements into the correct location:
Call coll2.reverse()
Call coll2.add(1.5)
Do
Print coll.getNthElementRaw(i)
Loop While ++i < coll.ElementCount
This will now print: