






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







VisualWorks provides several classes for operations involving collections of objects. We can use System Browser to explore the collection classes when we need a special kind of collection. The commonly used collections for CS 2340 are:
We create an empty collection, and then add elements to it. All collections respond to the new message.
Array allows you to maintain relative positions of elements, via an integer index. As an example, if a name were to be stored as a collection of three elementsโfirst, middle, and
last namesโit would make sense to use an Array because the relative positions of the elements must be preserved.
A ByteArray provides space-efficient storage for bytes. Its elements are restricted to the set of SmallIntegers from 0 to 255. WordArray is for manipulating 16-bit words; its elements can be integers from 0 to 65535 For an Array, which cannot add elements, it is necessary to specify the size of the array. Each element is nil until replaced with another object. | Sports | Sports := Array new: 4. Sports at: 1 put: 'Table Tennis'; at: 2 put: 'Cricket'; at: 3 put: 'Soccer'; at: 4 put: 'Football'. ^Sports. You can also create a collection by specifying up to four elements. This approach is typically used to create a small array. Variations of the with: message, for up to four elements, are provided in VisualWorks: When an array contains only literal elements, such as numbers and strings, you can also create the array using its literal form: | array1 array2 | array1 := #( 'A' 'B' 'C' D' ).
When elements are not added in the desired order, sorting is required. SortedCollection provides that extra capability. By default, elements are sorted in ascending order. You can override this default by specifying an alternative sort algorithm enclosed in a block. For example, The expression: SortedCollection sortBlock: [:x :y | x >= y] creates a new collection whose elements will be sorted in descending order. The block is given two elements to compare, and is expected to answer true when the first element should precede the second element. Sample use of sorted Collections: changeBlock: policyVal policyVal = 123 ifTrue: [lots sortBlock: [:a :b | a lotItemPrice value >=b lotItemPrice value]
The Dictionary class, instead of imposing an integer index on each element, permits any object to be the external key. For example, an element might consist of the word โobjectโ with the associated definition โsomething solid that can be seen or touchedโ. Thus, each element in a Dictionary is typically an instance of Association, which is a key-value pair.
To add an element to a Dictionary, send an at:put: message to the dictionary. The first argument is the lookup key (typically but not necessarily a Symbol). The second argument is the object to be associated with the key. | aussie | aussie := Dictionary new. aussie at: #Captain put: 'Ricky Ponting'; at: #Member1 put: 'Matthew Hayden'; at: #Member2 put: 'Adam Gilchrist'; at: #Member3 put: 'Glenn Mc Grath'. ^aussie
Collection classes which preserve order, such as OrderedCollection, support protocol for inserting elements at specific positions. It is sometimes helpful to make this position Explicit. To insert an element at the beginning of an ordered collection, send an addFirst: message, which the new element as the argument.
To insert an element before or after a specific element already in the collection, send an add: before: message or add: after: message to the collection. The first argument is the element to be inserted. The second argument is the element relative to which the insertion is to take place.
array := copy. ^array
The basic method for removing an object from a collection is to send a remove: message to the collection, with the object to be removed as argument. An empty block is an effective means of taking no action, so the process can continue without an error message or other action.
To remove occurrences of an object from an array, we can create a copy of the array, omitting each occurrence of a specified object. Send a copyWithout: message to the Array. The argument is the object to be removed. The copy can then be substituted for the original array. The copyWithout: message works for all ordered collections as well as arrays. | dates workingDays | dates := #( 1 8 3 4 5 6 7 8 9 ). workingDays := dates workingDays copyWithout: 8. dates := workingDays.
^dates
To test for emptiness, send an isEmpty message to the collection. The response is true when the collection has no elements and false otherwise. Similarly, you can test whether the collection is not empty by sending a notEmpty message.
Ordered collections provide several messages for removing a single element at a specified position or a range of elements: | numbers | numbers :=list new: 25. 1 to: 25 do: [ :i | numbers add: i]. numbers removeFirst. "Removes 1" numbers removeFirst: 5. "Removes 2 3 4 5 6" numbers removeLast. "Removes 25" numbers removeLast: 5. "Removes 20 21 22 23 24" numbers removeFrom: 8 to: 12. "Removes 14 15 16 17 18" ^numbers Arbitrary collections are sorted by first being converted to an instance of SortedCollection. asSortedCollection Returns a new collection as an instance of SortedCollection, with the collectionโs elements in ascending order. reverse Returns a new collection of the same kind, but with the elements in reversed order.
invento := Inventory new. i1 := Item new. i1 name: 'Video Game'. i1 id:'89X999'. l1:=Lot new. iNR:= Money new. iNR conversionRate: 51.3. iNR currencyName:'Indian Rupee'. l1 lotItemPrice: iNR. l1 numOfItems: 100. l1 prototype:i1. i2 := Item new. i2 name: 'TV'. i2 id:'69X89Y'. l2:= Lot new. won:= Money new. won conversionRate: 1000. won currencyName:'Won'. l2 lotItemPrice: won. testBestLotForItem | l3 GG| l3:= Lot new.
GG:= Money new. GG conversionRate: 20. GG currencyName:'Good Game'. l3 lotItemPrice: GG. l3 numOfItems: 100. l3 prototype:i1. invento pushLot: l3. invento pushLot: l1. invento pushLot: l2. self assert: ((invento bestLotForItem: i1) = l3).