| by Arround The Web | No comments

Swift Array – insert

In this Swift guide, we will see the insert() method in the Array Collection.

Before going to discuss this method, we will see what an Array is.

Swift Array

Array in Swift is a Collection that stores multiple elements with the same data type.

In Swift, we have to specify the datatype to represent the data type of Array while creating an array. It can also be possible to create elements in an Array without specifying any data type.

insert()

insert() in Array Swift can be used to add an element at a specified position. We can insert a particular element at a particular position using index value.

If there is any element existing at the index position and if we want to insert a new element at the same position, then a new element is inserted at that position. The existing element will be moved after. It takes two parameters.

Syntax:

swift_array.insert(Element,at:index)

 
Parameters:

    1. Element – If you want to add an element to an Existing Swift Array, you need to pass the Element as a parameter.
    2. at: It takes an index position which is an integer value that specifies the position where the Element is to be inserted.

Return:

It returns an Array with added elements.

Note – You have to add the element with respect to the same data type of the existing array.

insert() method also supports two direct value properties within a parameter.

If you want to add element at first position in the swift array, at: takes startIndex
Value. It inserts the value at first position.

Syntax:

swift_array.insert(Element,at:swift_array.startIndex)

 
If you want to add element at last position in the swift array, at: takes endIndex
Value. It inserts the value at the last position.

Syntax:

swift_array.insert(Element,at:swift_array.endIndex)

 

Example 1:

Create a Swift Array that stores two Integer values. Let’s add some elements at a particular position using insert() method.

//create an Swift Array with 2 integer elements
var swift_array:[Int] = [34,56]

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add 100 at second position
swift_array.insert(100,at:1)

print("Swift Array after adding 100: ", swift_array)

//add 200 at fourth position
swift_array.insert(200,at:3)

print("Swift Array after adding 200: ", swift_array)

//add 400 at first position
swift_array.insert(400,at:0)

print("Swift Array after adding 200: ", swift_array)

//add 300 at third  position
swift_array.insert(300,at:2)

print("Swift Array after adding 200: ", swift_array)

 
Output:

Explanation:

Line -2:


We created an Array named swift_array that holds 2 integer values.

34 is located in the 0th index and 56 is located at the 1st index.

Line -8:


We added value – 100 at index-1.

Line -13:


We added value – 200 at index-3.

Line -18:


We added value – 400 at index-0.

Line -23:


Finally, we added value – 300 at index-2.

So, the Updated Swift array is:

Example 2:

Create an empty Swift Array with string type. Let’s add some elements at a particular position using insert() method.

//create an empty Swift Array
var swift_array:[String] = []

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add "Linuxhint" at first  position
swift_array.insert("Linuxhint",at:0)
print("Swift Array after adding Linuxhint: ", swift_array)

//add "Swift" at second  position
swift_array.insert("Swift",at:1)
print("Swift Array after adding Swift: ", swift_array)

 
Output:

Explanation:

Line -2:


We created an Array named swift_array that holds no string values

Line 8-9:


We added a string “Linuxhint” at first position and displayed the Array.

Line -13-14:


We added a string “Swift” at second position and displayed the Array.

So, the Updated Swift array is:

Example 3:

Create an empty Swift Array with Integer type and add the first and last elements using startIndex and endIndex values.

//create an empty Swift Array
var swift_array:[Int] = []

// Actual swift array
print("Actual Swift Array: ", swift_array)

//add 10 at first  position
swift_array.insert(10,at:swift_array.startIndex)
print("Swift Array after adding 10: ", swift_array)

//add 20 at last  position
swift_array.insert(20,at:swift_array.endIndex)
print("Swift Array after adding 20: ", swift_array)

 
Output:

Explanation:

Line -2:


We created an Array named swift_array that holds no Integer values

Line 8-9:


We added a value – 10 at the first position and displayed the Array.

Line -13-14:


We added a value – 20 at the last position and displayed the Array.

So, the Updated Swift array is:

Conclusion

So, we saw how to insert an element to the swift array using insert() method. We can insert a particular element at a particular position using index value.

If there is any element existing at the index position and if we want to insert a new element at the same position, then a new element is inserted at that position and the existing element will be moved after. We discussed 3 different examples to understand this concept better. Make sure that you have to specify the Index value within the specified range only.

Share Button

Source: linuxhint.com

Leave a Reply