I would like to work in Ceylon with a multidimensional array. Is this planned in Ceylon? If so, how can I declare it?
I would like to use this construct in Ceylon, as shown here in Java:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[2][2] = "a string";
First, consider whether you really need an array (i.e. something with fixed length and modifiable elements), or whether a list (or list of lists) or a map might be better. Though from your example, you seem to need modification.
In the JVM, a "multidimensional" array is just an array of arrays.
In Java,
new String[y]creates an array filled withnullentries, which is not an allowed value of typeStringin Ceylon. So you can either have an array ofString?(which allowsnull), or pre-fill your array with some other value, using e.g. Array.ofSize(...):The same is valid for arrays of arrays. We could do this:
Though this would have the same array in each "row" of the 2D-array, which is not what we want (as replacing
myStringArray[2][2]would replace all entries with a 2 as the second coordinate). Here the other Array constructor is useful, which takes an iterable:This takes advantage of the fact that the iterable enumeration evaluates its arguments lazily, and thus the array constructor will get x different elements.