|
In most cases, the return value is an
Array
whose contents depend on the supplied arguments, though there is one set of conditions
that could result in a single
Object
value being returned.
In all cases,
a counter is maintained that starts at zero and is incremented each time a value in the
data
array differs from the preceding value.
Thus, a sequence of
int
values is generated with a length equal to the length of the
data
array.
When no additional arguments are supplied, that sequence becomes the returned
Array
of
int
values.
When one argument is supplied, the return value can be one of three possibilities that
depend on the data type of the argument.
When that argument,
arg1,
is an
int
value, it is used as a modulus value for a modulo operation that is applied to each
value in the counter sequence and this new sequence becomes the returned
Array
of
int
values.
When that argument
arg1,
is an
Array
value, the length of the array provide the modulus value that is applied as before,
but now the resulting sequence is used to select elements from that array which are
then used in lieu of the sequence value when constructing the returned
Array
of
Object
values, whose data types depend on the types stored in the
arg1
array.
When
arg1
is some other data type, namely neither an
int
nor an
Array,
then it simply becomes the return value.
This last case is the one instance where the returned value is not an
Array.
When additional arguments, namely three or more arguments are supplied, then
the returned value is always an
Array
where the first value is exactly what would have been returned if just
arg1
had been present,
the second value is exactly what would have been returned if just
arg2
had been present,
and so on.
This built-in can be particularly useful for shading the rows in a
JTable
after the rows are sorted.
In this case, it is useful to have the
data
argument be the result of a
TABLE_JOIN
action and the remaining arguments be an
unroll
of a snapshot of the initial value of the table's
cellcolors
Array.
For example, after a sort involving columns zero, two and three of a table, one could
group rows with identical values in those columns by means of shading as follows:
cellcolors = getArrayBands(
action(TABLE_JOIN, new Array[] { 0, 2, 3 }, 1, 1, 1),
unroll(initcellcolors));
where
initcellcolors
could have been set as follows:
Array initcellcolors = {
new Array[] { Color.yellow, Color.orange }, // background
Color.red, // foreground
};
| |
| Example: |
The following script demonstrates different calls to
getArrayBands
using the same sample data array.
import yoix.*.*;
PrintResult(String prefix, Array results, ...) {
Pointer ptr;
Pointer result;
printf("%s", prefix);
for (result in &results) {
if (*result instanceof Array) {
for (ptr in *result) {
if (*ptr instanceof int)
printf(" %d", *ptr);
else if (*ptr instanceof Color)
printf(" %s", getColorName(*ptr));
else if (*ptr instanceof Array)
PrintResult("", *ptr);
else printf(" %O", *ptr);
}
} else if (*result instanceof int)
printf(" %d", *result);
else if (*result instanceof Color)
printf(" %s", getColorName(*result));
else printf(" %O", *result);
printf("\n");
}
}
Array data = { "A", "B", "B", "C", "D" };
PrintResult("Data:\n", data);
PrintResult("Case 1:\n", getArrayBands(data));
PrintResult("Case 2:\n", getArrayBands(data, 3));
Array colors3 = { Color.red, Color.green, Color.blue,
Color.yellow, Color.orange };
PrintResult("Case 3:\n", getArrayBands(data, colors3));
Array colors4 = { Color.red, Color.green };
PrintResult("Case 4:\n", getArrayBands(data, colors4));
PrintResult("Case 5:\n", unroll(getArrayBands(data, colors4, Color.magenta, 2)));
The results on standard output are:
Data:
A B B C D
Case 1:
0 1 1 2 3
Case 2:
0 1 1 2 0
Case 3:
red green green blue yellow
Case 4:
red green green red green
Case 5:
red green green red green
magenta
0 1 1 0 1
| | |
| Return: |
Array
| | |
| See Also: |
JTable
|
|