swift Array append() Function



The append() function of an array is used to add a new element at the end of the specified array. It is the most commonly used function by the array to add an element in either a new or existing array. This function adds one element at a time. It can only work with mutating arrays, not with non-mutating arrays. Mutating arrays are those arrays whose value can be modified and they are assigned to a variable whereas non-mutating arrays are those arrays whose value cannot be modified and they are assigned to constants. The complexity of this function is O(1).

For example, we have an array = [30, 20, 11, 19]. Now using the append(3) function we add a new element = 3 at the end of the array. Hence the resultant array is [30, 20, 11, 19, 3].

Syntax

Following is the syntax for append() function.

func append(_item: Element)

Parameters

This function takes only one parameter which is the item. Here item represents the new element that we want to add at the end of the array.

Return value

This function does not return anything.

Now we will discuss how to use the append() function with the help of the following examples:

Example 1

Swift program to demonstrate how to append a new element at the end of the existing array using the append() function.

import Foundation

// Array of integer Type
var arr = [10, 32, 19, 29]
print("Existing array:", arr)

// Appending new elements in the given array
// Using append() function
arr.append(3)
arr.append(1)

print("Array after appending two new elements:", arr)

Output

Existing array: [10, 32, 19, 29]
Array after appending two new elements: [10, 32, 19, 29, 3, 1]

Example 2

Swift program to demonstrate how to add elements in the empty array and a new array using the append() function.

import Foundation

// Empty array
var emptyArr: [Double] = []

// Appending new elements using append() function
emptyArr.append(10)
emptyArr.append(23)

print("Array after appending new elements: \(emptyArr)")

// New array of string type
var newArr: [String] = []

// Adding elements to the new array using the append() function
newArr.append("Swift")
newArr.append("Java")
newArr.append("C++")
newArr.append("C#")

print("New array: \(newArr)")

Output

Array after appending new elements: [10.0, 23.0]
New array: ["Swift", "Java", "C++", "C#"]

Example 3

Swift program to create a new array that only contains even numbers.

import Foundation

// Empty array to store the even numbers
var resultantArr: [Int] = []

// Appendding only even numbers to the array
for even in 10...50 {
    if even % 2 == 0 {
        resultantArr.append(even)
    }
}

print("Array of even numbers: \(resultantArr)")

Output

Array of even numbers: [10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]

Example 4

Swift program to append multiplication table in the array.

import Foundation

// 2-D array to store multiplication table
var table: [[Int]] = []

// Defining the size of the multiplication table
let rows = 6
let columns = 6

for x in 1...rows {

    var storeRow: [Int] = []
    for y in 1...columns {
    
        // Calculating the product and append the result in the rows
        storeRow.append(x * y)
    }
    
    // Appending rows in the 2-D array
    table.append(storeRow)
}

// Displaying the multiplication table
for x in table {
    print(x)
}

Output

[1, 2, 3, 4, 5, 6]
[2, 4, 6, 8, 10, 12]
[3, 6, 9, 12, 15, 18]
[4, 8, 12, 16, 20, 24]
[5, 10, 15, 20, 25, 30]
[6, 12, 18, 24, 30, 36]
Advertisements