Dynamic Arrays

By default, arrays have a fixed size.

let scores = 3, 2, 5 //size: 3
let scores: Int[3]   //size: 3

You can use the ? symbol to set the array size to whatever it first gets assigned to (this is the default behaviour).

let scores: Int[?] = 3, 2, 5 //size: 3
let Person = [
    name: String
    scores: Int[?]
]

let lu = Person "Lu" (3, 2, 5) //scores size: 3
let bob = Person "Bob" (4, 6)  //scores size: 2

You can make an array re-sizeable by leaving the size empty.

let scores: Int[] = new (3, 2, 5)
scores:push(4)

Last updated

Was this helpful?