10 Arrays
BeginnerArrays are declared as Array<T> where T represents the type. T can be any type. Arrays are a reference type variables
C is initialised with 3 elements of value 0
Array d values can be initialised using lamda expressions.
You can fetch size of array.
This syntax allows you to assign e the values in position 1 and 2 of array d.
VArrays, are value type arrays that live on the stack, reduces pressure of memory allocation and garbage collection on the heap but introduces extra performance overhead. Therefore, you are advised not to use long VArray in performance sensitive scenarios when the size is known at compile-time.
arrays.cj
main() {
// Array whose element type is Int64
var a: Array<Int64> = [1, 3, 2, 4]
// Array whose element type is String
var b: Array<String> = ["Hello", "World"]
let c = Array<Int64>(3, item: 0)
for (x in c) {
print("${x} ")
}
print('\n')
let d = Array<Int64>(3, {i => 2 * i + 1})
for (x in d) {
print("${x} ")
}
print('\n')
println("d has ${d.size} elements")
let e = d[1..3]
for (x in e) {
print("${x} ")
}
print('\n')
let vA: VArray<Int64, $3> = [4, 5, 1]
}
// Output:
// 0 0 0
// 1 3 5
// d has 3 elements
// 3 5