Introduction
In this article, we will learn the basic and fundamental concept of an array in Swift language. As far as the concept of an array is concerned, it is the same in other languages, but if we are talking about the advanced level; the manipulating of an array is different.
What is an Array?
- An array is an ordered collection of the similar data type. The element in an array starts with zero index, moves up beyond the means and we store the first value on index zero and likewise.
Why is array useful?
- If you want to store the element which has the same data type, you use an array. Suppose you have an array with 100 elements and you want to retrieve the elements which are at position 50. Thus, without iterating the entire array, you want only the index of position 50 and your value, present at index 50, is retrieved through an array.
Mutable Vs Immutable Array
- If you have read the previous article, you can see that we declare a variable using the simple keyword variable or declare with the constant keyword. Thus, you have an option. If you want to declare an array which doesn’t need to change after creating an array , you should make it immutable, using the constant keyword let. If you want to add, remove or update the values in an array, you should make it a mutable array by using the var keyword.
How do we create an array?
- Explicit Declaration- You can declare an array explicitly. I am giving you an example of an explicit array:
- In this example, we declare an integer type of an array, naming it as the numbers and using the keyword let means its value can’t be changed after creating.
- Inferred Declaration: Swift can also give flexibility to infer the type of an array from the type of an initializer.
- You can see that in this example, we can make an integer type array which is now empty. Suppose, we add one element in index zero, what will happen?
- You can see that when I give the value at zero, it gives an error. Why? Because, if you see a constant array, the value can’t be changed, added or removed from any array. In this we found an error, and if we change let into var, what will happen?
- You will see that after changing the keyword let into the var array, it takes our value.
- Shorter Declaration: Another shorter way to declare an array is that you use the square brackets.
- In this example, you see that we do the same work which is done with previous examples but in this example we declare an array in some different way -- we also use this method to declare an array.
What are Array Literals?
- An array literally refers to the list which is separated from the commas and is enclosed in the brackets.
Accessing Array Element
- Suppose, we have an array. Now, we will access its first value. We have two options;the first is that we access its index andthe second is use its built-in method. Now, in this section, we will use the Swift built-in method.
- Suppose we want to check if an array is empty or not, we use the built-in method isEmpty(). This method gives only a Boolean value.
- Suppose we want to access the first element in an array, so we use first() method to access the first element in an array. In the print statement, you will find that in the round brackets, we give an array name and call the first method.
- Suppose, we want to access the last element in an array, so we use last() method to access the last element in an array. In the print statement, you will see that in the round brackets, we give an array name and we call the last method.
- Suppose, we want to access the maximum/large element in an array, we use maxElement() method to access the max element in an array. In the print statement, you will see that in the round brackets, we give an array name and call the maxElement method.
- Suppose we want to access the minimum/small element in an array, so we use minElement() method to access the max element in an array. In the print statement, you will see that in round brackets, we give an array name and call the minElement method.
Using Subscripting
- Now, we will discuss a second method to call a specific value of an array. Now, we will use an index-based approach in an array.
- In this example, we make an array and call the first element by giving the index of it.
- If we give an index which is out of an array range, we will immediately get an error.
- We can also use the range of an array to call the element of any array. Likewise, I am giving you an example which enables you to understand the concept of the range. In this example, we will give the starting index and the last range, where we need to retrieve the array values and between this, we will declare the three dots, which is an optional task.
Manipulating Elements in an Array
- Suppose we want to add a new element in an array. For this purpose, we use append() method to add an element into an array. In this example, you will see the number 228 is added in an array and I printed the array before addition and after addition.
- Now, if you again add a new element in an array, the second time, we can’t use an append method. We will solve this problem with some other method like:
- If we want to insert an element in an array you use insert() method for this purpose.
Removing Element in an Array
- If we want to remove the last element in an array, we use the built-in method removeLast().
- If we want to remove the element through any index in an array, we use built-in method removeAtIndex().
Updating element in an Array
- If we want to update the element in an array, we simply give the array index where we want to update and give its value, and the array is updated.
Main Point of an Array
- Arrays are the ordered collection of the values with similar type of data
- Using index, we can only update and access the element of an array
- Arrays are the value type, so when we assign it into the new variable, they are copied into the new variable.