Array is an
Ordered Collection Type and array is used to organize data where in some instances used to hold elements of single type.
CREATE ARRAY WITH VALUE
In order to create an array, you could do either of these:
let numbers = [1, 2, 3, 4, 5]
let names = ["Kelvin", "Beam", "Lexton"]
The examples above uses
let which means you won’t be able to modify it’s content. If you would like modify it’s content such as adding or removing, consider using
var. Anyway, spot the differences between
String in the use of array.
String requires the use of double quotation while
Int does not. These are also array with value meaning numbers array contains 1, 2, 3, 4, 5 while names array contains Kelvin, Beam and Lexton.
CREATE AN EMPTY ARRAY
We’ve seen how to create array with value in it. Now let’s create array without any value in it. The code below basically means, this is an array of
Int only. And if you try adding
String in it, it will crash.
var numbers: [Int] = 
ADDING INTO ARRAY
Let’s say we would like to add some numbers into our array. We’ve added 10 into our array and now our array should be containing only 10.
And if we would like to add more, we could simply add more with the command of
Now our array should contain 10, 11 and 13.
REMOVING FROM ARRAY
Remember that in array, the first number is always 0 and this is how we retrieve the value of the array. Let’s say we would like to remove 10 from the array, we could do this.
And if we would like to remove 11, we could do. Now if we have already remove 10, then 11 would decrement into 0 instead of 1.