As you probably know, arrays on the JVM are special objects. Golo deals with such arrays as being
instances of Object[]
and does not provide a wrapper class like many languages do. A Java / JVM
array is just what it is supposed to be.
Golo adds some sugar to relieve the pain of working with arrays. Golo allows some special methods to be invoked on arrays:
get(index)
returns the value at index
,
set(index, value)
sets value
at index
,
length()
and size()
return the array length,
iterator()
returns a java.util.Iterator
,
toString()
delegates to java.util.Arrays.toString(Object[])
,
asList()
delegates to java.util.Arrays.asList(Object[])
,
equals(someArray)
delegates to java.util.Arrays.equals(this, someArray)
,
getClass()
return the array class.
Given a reference a
on some array:
# Gets the element at index 0 a: get(0) # Replaces the element at index 1 with "a" a: set(1, "a") # Nice print println(a: toString()) # Convert to a real collection let list = a: asList()
The methods above do not perform array bound checks.
Finally, arrays can be created with the Array
function, as in:
let a = Array(1, 2, 3, 4) let b = Array("a", "b")
You can of course take advantage of the array
collection literal, too:
let a = array[1, 2, 3, 4] let b = array["a", "b"]