You represent values and items in a series in G2 by using:
![]() |
Your application can dynamically insert items and values into and remove items and values from a list or array. Your application can also test whether an item or value is a member of a particular list or array as a condition for further processing.
List and Array Class Hierarchy
Similar to variables and parameters, G2 provides build-in list and array classes that correspond to the basic data types.This figure shows the hierarchy of built-in list and array classes:
![]() |
Creating a Subclass of List or Array
Just as you can create subclasses of the built-in variable and parameter classes, you can create subclasses of the built-in list and array classes. You would create a subclass of an item array, for example, if your application requires several instances of an array that contains the same class of items.
To create a subclass of list or array:
Attribute-initializations for the class to initialize the Initializable-system-attributes, as needed.
Element-type of lists and arrays, which determines the value type or class of the list or array elements
List-is-permanent or Array-is-permanent of lists and arrays, which determines whether G2 deletes the elements in the array upon reset
Array-length and Initial-values of arrays, which specifies the default length and initial values of the array
Element-type attribute initialization of the item list or item array subclass.
To declare an attribute to contain a subclass of list or array:
In the Class-specific-attributes of the class whose attribute contains a list or array, use the initially is an instance of syntax to refer to the list or array class.
list-of-objects initially is an instances of a permanent-item-array
permanent-item-array is a subclass of the built-in G2 class item-array.
L[1000], are many times slower for lists than the equivalent array reference. The reason is G2 counts through the list element-by-element until it reaches the 1000th element.
L[1000] to access the elements of a list, unless the list is guaranteed to have less than 500 elements.The exception to this guideline is accessing the first element of a list by its index when the list contains more than 500 elements. For example, list[0] is only slightly slower than the same reference for an array.
These operations are much more efficient for lists than for arrays:
For more information, see the references in this table:
| For information on... | See... |
|---|---|
|
Casting
|
Use Casting When Binding Array Subclasses to Local Variables
|
|
Supporting data for efficiency conclusions
|
Appendix A, Benchmarking Results
|
Guidelines for Using Lists
Follow these guidelines when using lists. Use Lists for Fast Insertion and Removal
You often need to maintain a list of items, when the number of items is not known or might vary significantly at run-time. For example, if you needed to maintain a list of messages that the application generates at run-time, you would use a list, because you rarely need to access a specific element by its index. insert at the beginning of or insert at the end of actions. If you can identify a specific item without referring to its index, for example, by using a relation, by iterating through the elements, or by using a unique identifier, you can also use the insert before and insert after actions to insert elements.
For more information about the performance implications of casting, see Effect of Subclassing, Stability, and Casting on Array References.
Choose the Most Restrictive Data Type
When choosing the superior class for a list subclass, you should always choose the most restrictive class for the type of data.
Guidelines for Using Arrays
If you determine that your application requires an array, follow these guidelines when using the array. Use Arrays for Fast Access to Elements by Index
Due to the inefficiency of referencing list elements by index, you should almost always use an array if you need to reference or assign a specific element by its index. For example, you typically use arrays for fast numeric operations and matrix operations.
Always Change Array Elements, Never Conclude
Assigning values to an array is extremely fast when you use the change action. In fact, changing array elements is significantly faster than concluding attributes, parameters, or variables, and is almost as fast as concluding a local variable. However, concluding an array element is significantly slower than changing an array element, although they are functionally equivalent.
Caution: Never use the conclude action to change the elements of an array; always use the change action.
Beware of Manipulating Array Elements
When you manipulate the elements of an array, be aware of the following:
For a detailed explanation, see Use Strong Typing.
Use Stability Declarations for Value Array Subclasses
For maximum efficiency, when you create a subclass of any type of value array, you should declare the class stable for dependent compilations.
Use Native Arrays in Performance-Sensitive Code
Accessing values from a native array is faster than getting values from a subclassed array. Thus, you should use native arrays in performance-sensitive code. The difference between native and non-native lists is less significant. Use Casting When Binding Array Subclasses to Local Variables
Casting is when you bind a local variable to a subclass of the local variable's type, as opposed to declaring a local variable explicitly for the subclass. In the case of arrays, this means declaring a local variable to be a native array class, and binding the local variable to an array subclass. Casting can improve performance under these circumstances:
This figure shows an example of binding a local variable with casting and without. In the first procedure,
FA is declared to be a type of my-float-array. The procedure then creates an instance of my-float-array, which is the same type as the local variable. In the second procedure, FA is declared to be a type of float-array, which is a superior class of my-float-array. The procedure then creates an instance of the subclass and binds it to the local variable, using casting.
![]() |