NamedArraylist class: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 6: | Line 6: | ||
For background information about collections and NamedArraylists and about | For background information about collections and NamedArraylists and about | ||
declaring NamedArraylist object variables, | declaring NamedArraylist object variables, see [[Collections]]. | ||
see [[Collections]]. | |||
See also [[Collections#Coding considerations for collections|Coding | See also [[Collections#Coding considerations for collections|Coding | ||
considerations for collections]]. | considerations for collections]]. | ||
The following code illustrates some of the NamedArraylist methods: | The following code illustrates some of the NamedArraylist methods: | ||
< | <p class="code">%narl is collection NamedArraylist of longstring | ||
%i is float | |||
%narl = new | |||
%narl:useDefault = true | |||
%narl('Idle') = 'Eric' | |||
%narl('Cleese') = 'John' | |||
%narl('Gilliam') = 'Terry' | |||
%narl('Pallin') = 'Michael' | |||
%narl('Chapman') = 'Graham' | |||
Print 'The American''s first name is ' %narl('Gilliam') | |||
print '*** Completely different:' | |||
for %i from 1 to %narl:count | |||
print %narl:itemByNumber(%i) and %narl:nameByNumber(%i) | |||
end for | |||
</ | </p> | ||
This code prints: | This code prints: | ||
< | <p class="output">The American's first name is Terry | ||
*** Completely different: | |||
Graham Chapman | |||
John Cleese | |||
Terry Gilliam | |||
Eric Idle | |||
Michael Pallin | |||
</p> | |||
</ | |||
: | '''Note:''' The items are kept in EBCDIC order by their name. | ||
The items are kept in EBCDIC order by their name. | |||
This is always the case for NamedArraylist items. | This is always the case for NamedArraylist items. | ||
Line 56: | Line 53: | ||
NamedArraylists provide an ideal way of getting the total quantity | NamedArraylists provide an ideal way of getting the total quantity | ||
of each item in the recordSet: | of each item in the recordSet: | ||
< | <p class="code">%quantity is collection NamedArraylist of float | ||
... | |||
%quantity = new | |||
%quantity:useDefault = true | |||
for each record in %recset | |||
%quantity(productId) = %quantity(productId) + quantity | |||
end for | |||
for %i from 1 to %quantity:count | |||
print %quantity:nameByNumber(%i) and - | |||
%quantity:itemByNumber(%i) | |||
end for | |||
</ | </p> | ||
Because UseDefault is set to <tt>True</tt>, the first reference to | Because UseDefault is set to <tt>True</tt>, the first reference to | ||
a %quantity item for a given Productid will return the default | a %quantity item for a given Productid will return the default | ||
Line 91: | Line 88: | ||
prove useful, without concern for performance (which will be quite good). | prove useful, without concern for performance (which will be quite good). | ||
==List of NamedArraylist methods== | |||
The [[List of NamedArraylist methods|"List of NamedArraylist methods"]] shows all the class methods, with a brief description of each. | |||
==See also== | ==See also== | ||
<table> | <table> | ||
<tr><th>[[List of FloatNamedArraylist methods]]</th> | <tr><th>[[List of FloatNamedArraylist methods]]</th> | ||
<td>For a list of all methods in the <var>FloatNamedArraylist</var> class, with a brief description of each.</td></tr> | <td>For a list of all methods in the <var>FloatNamedArraylist</var> class, with a brief description of each.</td></tr> | ||
Line 112: | Line 104: | ||
<td>For tips on using collections.</td></tr> | <td>For tips on using collections.</td></tr> | ||
</table> | </table> | ||
[[Category:System classes]] | [[Category:System classes]] |
Revision as of 21:44, 23 December 2011
NamedArraylists can be thought of as traditional arrays with no bounds but with identifying subscripts that can be arbitrary strings rather than numbers.
The NamedArraylist class extends the system GenericNamedArraylist class, so many of the methods available in the NamedArraylist class are documented as belonging to the GenericNamedArraylist class. Any method available in the GenericNamedArraylist class is also available in the NamedArraylist class. The other classes that extend the GenericNamedArraylist class are the FloatNamedArraylist class and the UnicodeNamedArraylist class.
For background information about collections and NamedArraylists and about declaring NamedArraylist object variables, see Collections. See also Coding considerations for collections.
The following code illustrates some of the NamedArraylist methods:
%narl is collection NamedArraylist of longstring %i is float %narl = new %narl:useDefault = true %narl('Idle') = 'Eric' %narl('Cleese') = 'John' %narl('Gilliam') = 'Terry' %narl('Pallin') = 'Michael' %narl('Chapman') = 'Graham' Print 'The Americans first name is ' %narl('Gilliam') print '*** Completely different:' for %i from 1 to %narl:count print %narl:itemByNumber(%i) and %narl:nameByNumber(%i) end for
This code prints:
The American's first name is Terry *** Completely different: Graham Chapman John Cleese Terry Gilliam Eric Idle Michael Pallin
Note: The items are kept in EBCDIC order by their name. This is always the case for NamedArraylist items.
For another example, suppose there is a recordSet (?? refid=filobj.) in a file where field ProductId has a product ID and field Quantity has the number of items with that ID. The product IDs are ten-character alphanumeric IDs, so they cannot be used as numeric subscripts. Some records may have the same ProductId as others.
NamedArraylists provide an ideal way of getting the total quantity of each item in the recordSet:
%quantity is collection NamedArraylist of float ... %quantity = new %quantity:useDefault = true for each record in %recset %quantity(productId) = %quantity(productId) + quantity end for for %i from 1 to %quantity:count print %quantity:nameByNumber(%i) and - %quantity:itemByNumber(%i) end for
Because UseDefault is set to True, the first reference to a %quantity item for a given Productid will return the default value of 0. The value of the Quantity field is added to this 0, and the total is stored under the product ID. Subsequent requests for the same product ID will return the current running total for that product.
Looking up an item in a NamedArraylist by its name is only slightly (possibly immeasurably) more CPU intensive than looking up the item by number, either in a NamedArraylist or in an Arraylist. NamedArraylist items will also take up a bit more space in CCATEMP than Arraylist items — the extra space required to hold the names. Usually, this overhead should be minor.
Because of their relatively low extra overhead, NamedArraylists should be used wherever their functionality may prove useful, without concern for performance (which will be quite good).
List of NamedArraylist methods
The "List of NamedArraylist methods" shows all the class methods, with a brief description of each.
See also
List of FloatNamedArraylist methods | For a list of all methods in the FloatNamedArraylist class, with a brief description of each. |
---|---|
List of UnicodeNamedArraylist methods | For a list of all methods in the UnicodeNamedArraylist class, with a brief description of each. |
Collections | For background information about collections and Arraylists and about declaring Arraylist object variables. |
Coding considerations for collections | For tips on using collections. |