1. 首页
  2. 易语言 - EasyLanguage
  3. Arrays
  4. 数组/数组(保留字)Arrays/Arrays (Reserved Word)

数组/数组(保留字)Arrays/Arrays (Reserved Word)

Description:

Used to declare variable names that can contain multiple data elements.

The declaration of an array creates a name that refers to a grouped set of data elements within an analysis techniques or strategy; these arrays can be numeric values, true/false comparisons, or text. Arrays allow you to group and reference data elements with an index number. Like variables you can organize and annotate your code with functional names that describe the nature of the calculation or purpose of the data.

Arrays in EasyLanguage start at data element 0, however, all built-in functions that operate on arrays ignore the zero element, so it is advised not to use the zero element.

An array may be described as a table of variables. That is, an array can hold many values just as a single variable holds a single value. Using an array has the advantage of allowing manipulation of the values in all or part of the array at once. That is, all the values can be averaged or sorted as a group. This would be a much more difficult task with individual variables. Additionally, an array is convenient for storing and retrieving values that occur on intermittent or non-consecutive bars.

The elements in an array may be organized in a single dimension or multiple dimensions. Once again, in spreadsheet terms, a 1-dimensional array has 1 column; a 2-dimensional array has multiple columns and rows; a 3-dimensional array has two 2-dimensional spreadsheets one on top of the other.

The Array reserve word has two forms: Arrays and Array, each is functionally equivalent and each must be followed by a colon : then a list of array names separated by commas ( , ) .

Array names like variables are unique to the study they are declared in; you can use the same name over again in any other study, but you cannot use the same name within the same study, for example, declaring a array with the same name as an variable. Also, remember to avoid naming arrays with the same name as an EasyLanguage reserved word.

Usage:

Array: ArrayName(Value);

where…

ArrayName is the unique name of the user-declared array.
Value is either a Numeric, True/False or String value used as the initial value of the array.

OR

Array: <IntraBarPersist> <DataType> ArrayName(InitialValue<,datan>) <, <IntraBarPersist> <DataType> VarName(InitialValue<,datan>)> ;

(The angled brackets used in the declarations below denote that the parameter is optional. The angled brackets themselves are never typed into EasyLanguage code.)

where…

ArrayName is the array declaration statement that may alternately be typed as Arrays.
IntraBarPersist indicates that this array’s values can be updated on every tick. By default, array values are only updated at the close of each bar.
DataType is the optionally supplied data type (float, double, int, bool, string) of the array, typically used to conserve memory.
Name is the user specified array name that can be up to 20 characters long and must begin with an alphabetic character (names are not case sensitive)
InitialValue is the initial value of the array elements
datan is an optional parameter that identifies the data stream the array is tied to and may be data1 through data50.

Fixed Length Arrays

The declaration of a fixed length array must specify the maximum element reference number, and given an initial default value for each element; arrays are generally initialized to 0, but can be initial set to any useful value.

Examples
Usage Example: (Array Declaration, Single)

Array: int WeeklyHighs[52](0), int WeeklyLow[52](0)

(declares a single dimension 53 element array of integers, 0 to 52, and initializes each element to 0)

Usage Example: (Multidimensional Array Declaration)

Arrays: VolumeHighs[5,20](0), VolumeLow[5,20](0);

(declares a two dimensional array, 6 elements by 21 elements, or 126 elements, and initializes each element to 0)

Remember to avoid using data element 0 if you are going to use any of the built-in array functions.

Dynamic Arrays

The declaration of a dynamic array is the same as that of a fixed array except that the maximum element reference number is left blank. At this time, only single dimension dynamic arrays are supported.

Example
For example, to declare a dynamic array of integers named MyDynamicIntArray you would type the following into your EasyLanguage analysis technique:

Array: int MyDynamicIntArray[](0) ;

And to set the third element to 1 you would have to first increase the size of the array and then assign the value as follows:

Array_SetMaxIndex(MyDynamicIntArray, 10) ;

MyDynamicIntArray[2] = 1 ; //note that [2] is the third element since arrays are zero-based

Other than having to resize the dynamic array prior to use dynamic arrays will behave exactly as existing arrays do. Note that while you can use a dynamic array in place of an existing array the reverse is not true. The new dynamic array functions will not accept static arrays as inputs which is due to the fact the dynamic and static arrays are treated differently internally.


更新于 2020年5月29日

这篇文章对您有帮助吗?

相关的话题

留言评论