Variable Arrays

MJ represents a MAPPER variable array as java.util.List to leverage the flexibility and utility of Java collections. For example:

// @LDA <bigstring>s25[10] .
List<MJString> bigstring = Arrays.asList(new MJString[10]);

Although generic collections are a handy feature of Java, lists that conform to MAPPER's rules for variable arrays should be declared as containing variables of the same, concrete class: MJInteger, MJDecimal, MJString, MJDate or MJVariant.

Immutable Arrays

In the example above, the list directly wraps the array of variable references and is immutable. List elements cannot be removed or added, but any existing element can be set:

// LDV <name>s25='John Q. Public' .
MJNamedString name = new MJNamedString("name", VariableScope.LOCAL, MaprptVariableType.STRING,
  25, EnumSet.of(LoadOption.TRUNCATE), "John Q. Public");

// LDV <address>s25='123 Poplar Avenue' .
MJNamedString address = new MJNamedString("address", VariableScope.LOCAL, MaprptVariableType.STRING,
  25, EnumSet.of(LoadOption.TRUNCATE), "123 Poplar Avenue");

// LDV <phone>s25='202-555-1212' .
MJNamedString phone = new MJNamedString("phone", VariableScope.LOCAL, MaprptVariableType.STRING,
  25, EnumSet.of(LoadOption.TRUNCATE), "202-555-1212");

// @LDA <bigstring>=<name>,<address>,<phone> .
int idx = 0;
bigstring.set(idx++, name);
bigstring.set(idx++, address);
bigstring.set(idx++, phone);

Zero-based vs. One-based Subscripts

Note that Java array subscripts and indices start from 0, and not 1 like MAPPER. Using MJSubscriptProxyFactory,  MJ applications wishing to retain one-based subscripts can wrap a java.util.List with a proxy that supports "counting from one" instead of zero:

// @LDA <starr>h18[5] .
List<MJString> starr0 = Arrays.asList(new MJString[5]);
List<MJString> starr1 = MJSubscriptProxyFactory.countFromOneListProxy(starr0);
// Set first element in array
starr0.set(0, new MJString(VariableScope.LOCAL, MaprptVariableType.HOLLERITH, 18,
  EnumSet.of(LoadOption.TRUNCATE), "First"));
// First element in proxy list (from countFromOneListProxy()) occurs at index 1
assert starr0.get(0) == starr1.get(1);

Mutable Arrays

While MAPPER doesn't allow the size of an array to change after declaration, a re-hosted application may create a mutable list, allowing the list to shrink or grow as needed:

// @LDA <strList>s30[8] .
List<MJString> strList = new ArrayList<MJString>(Arrays.asList(new MJString[8]));
// Remove the first element in array
strList.remove(0);
// Add element at the end of array
strList.add(new MJString(VariableScope.LOCAL, MaprptVariableType.STRING, 30,
  EnumSet.of(LoadOption.TRUNCATE), "tail"));

Initialization

An initialized list is typically created from an initialized array:

// @LDA <qty>i6[5]=1,12,123,1234,12345 .
List<MJInteger> qty = new ArrayList<MJInteger>(Arrays.asList(new MJInteger[] {
  new MJInteger(VariableScope.LOCAL, 6, 1L), new MJInteger(VariableScope.LOCAL, 6, 12L),
  new MJInteger(VariableScope.LOCAL, 6, 123L), new MJInteger(VariableScope.LOCAL, 6, 1234L),
  new MJInteger(VariableScope.LOCAL, 6, 12345L)
}));

Named and Numbered Arrays

Like MAPPER variables, MAPPER arrays may be named or numbered, and named arrays may be global, environmental or local. MJ supports named or numbered variable arrays and array scope via variable namespaces. Continuing the bigstring examples shown above:

MJVariableNamespace namespace = new MJVariableNamespace(VariableScope.LOCAL);
namespace.addArray("bigstring", bigstring);
// Retrieve array from namespace as generic MJ variable.
List<? extends MJVariable> foundList = namespace.lookupArray("bigstring");
// Retrieve array from namespace as narrowed type.
List<? extends MJString> foundAsStrList = namespace.lookupArray(
  "bigstring", MJString.class);
// Retrieve array from namespace as narrowed, named type.
List<? extends MJNamedString> foundAsNamedStrList = namespace.lookupArray(
  "bigstring", MJNamedString.class);

There are no named or numbered variable array classes in MJ as there for named and numbered variables (MJNamedInteger and MJNumberedString, for example). An array (java.util.List) may be comprised of MJ variable instances that implement INamedVariable or INumberedVariable or "anonymous" classes like MJInteger and MJString. Regardless, the array itself is only known by the name or number associated with the array in the namespace. This is true for scope as well; the scope of an array is defined only by the namespace to which the array belongs.