next up previous contents index
Next: Writing loops Up: Arrays Previous: Arrays

Declaring and using arrays

As we mentioned above, it is possible to declare arrays of components and attributes. A component declarator for a component array takes one of the following forms:

       Identifier DimExprs 
       Identifier DimExprs ConstraintBlock
       Identifier DimExprs { ConstraintBlocks }
where DimExprs, ConstraintBlock and ConstraintBlocks are defined as follow:
       DimExprs:              '['Expression']'...'['Expression']'
       Constraints:           Constraint;...,Constraint
       ConstraintBlock:       { Constraints }
       ConstraintBlocks:      ConstraintBlock,...,ConstraintBlock
When there is only one constraint block following DimExprs, the constraints apply to all the array elements. In contrast, when there are multiple constraint blocks following DimExprs (the number of the constraint blocks must be the same as the size of the first dimension), the first constraint block applies to the first array element, the second one applies to the second array element, and so on.

An attribute declarator for an attribute array has the following form:

       Identifier DimExprs
       Identifier DimExprs in D
       Identifier DimExprs R Expression
The domain constraint and the arithmetic constraint following DimExprs apply to all the array elements.

The following shows three examples:

       component Button bts[10]{width == 30; height==30};
       component Label lbs[3]{{text == "zero"},
                              {text == "one"},
                              {text == "two"}};
       attribute int qs[10] in 0..9;
The first declaration declares ten buttons bts[0],...,bts[9] that have the same size . The constraints width == 30; height==30 apply to all the ten buttons. The second declaration declares three labels, with the first label's text being "zero", the second being "one", and the third being "two". The last one declares ten attribute variables in the domain of 0..9.

Elements of component and attribute arrays can be referenced in the same way as array elements in Java.



Neng-Fa ZHOU
Sat Apr 18 16:14:29 JST 1998