
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Copy All Elements of One Array to Another in Swift
In this article, we will learn how to write a swift program to copy all the elements of one array to another array. Swift doesn't provide a direct method to copy the elements of one array to another but we can do this using the following methods ?
Using map(_:) method
Using = operator
Using append(contentsOf:) method
Method 1: Using map(_:) method
The map(_:) method is used to return an array that contains the result of mapping the given closure over the specified sequence's elements.
Syntax
func map<T>(_mclosure:(Self.Element) throws - >T)rethrows [T]
Here, mclouser is a mapping closure, it accepts the elements of the specified sequence and return a transformed value of the same or different type.
Algorithm
Step 1 ? Create an array with values.
Step 2 ? Create another array of same type but empty.
Step 3 ? Copy the elements of array1 into array 2 using map(_:) function.
Array2 = Array1.map { $0 }
Step 4 ? Print the output.
Example
Following Swift program to copy all the elements of one array to another array.
import Foundation import Glibc let Array1 = [11, 22, 33, 44, 55] var Array2 = [Int]() // Copying the element of array1 into array2 // Using map Array2 = Array1.map { $0 } print("Original array: ", Array1) print("New array: ", Array2)
Output
Original array: [11, 22, 33, 44, 55] New array: [11, 22, 33, 44, 55]
Here in the above code, we have two arrays named Array1 with values and Array2 without values. Now to copy the elements of Array1 into Array2 we pass a closure that is {$0} in the map(_:) function which copies all the elements of Array1 starting from index 0 to the last index into the Array2.
Method 2: Using = operator
To copy the elements of one array to another we can also use the assignment operator. This operator assigns the values of the right-hand side operand to left and side operand.
Syntax
Array2 = Array1
Here, both Array1 and Array2 are of same type and the assignment operator assign the elements of Array1 to Array2.
Algorithm
Step 1 ? Create an array with values.
Step 2 ? Create another array of the same type but empty.
Step 3 ? Copy the elements of array1 into array 2 using the assignment operator.
Darray = Sarray
Step 4 ? Print the output.
Example
Following the Swift program to copy all the elements of one array to another array
import Foundation import Glibc let Sarray = [10, 20, 30, 40, 50] // Create an empty array with the same type as Sarray var Darray = [Int]() // Copy all the elements of Sarray to Darray Darray = Sarray print("Array 1:", Sarray) print("Array 2:", Darray)
Output
Array 1: [10, 20, 30, 40, 50] Array 2: [10, 20, 30, 40, 50]
Here in the above code, we have two arrays named Array1 with values and Array2 without values. Now to copy the elements of Array1 into Array2 we use the assignment operator. This operator assigns all the elements of Array1 into Array2.
Method 3: Using append(contentsOf:) Method
To copy the elements of one array to another we can also use append(contentsOf:) method. This method adds the elements of the given array or sequence to the end of the new array.
Syntax
func append<S>(contentsOf nElement: S) where Element == s.Element, S.Sequence
Here, nElement is the sequence whose elements are going to append into the specified array.
Algorithm
Step 1 ? Create an array with values.
Step 2 ? Create another array of the same type but empty.
Step 3 ? Copy the elements of array1 into array 2 using append(contentsOf:) method.
Array2.append(contentsOf: Array1)
Step 4 ? Print the output.
Example
Following Swift program to copy all the elements of one array to another array
import Foundation import Glibc let Array1 = ["Ox", "Cow", "Bee", "Cat", "Dog"] // Create an empty array with the same type as array1 var Array2 = [String]() // Copy all the elements of array1 to array2 // Using append(:) method Array2.append(contentsOf: Array1) print("Original array:", Array1) print("New array:", Array2)
Output
Original array: ["Ox", "Cow", "Bee", "Cat", "Dog"] New array: ["Ox", "Cow", "Bee", "Cat", "Dog"]
Here in the above code, we have two arrays named Array1 with values and Array2 without values. To copy the elements of Array1 into Array2 we use append(contentsOf:) method. In this method, we pass Array1 as the value of the contentsOf parameter. Now, this method append all the elements of Array1 into Array2.
Conclusion
So this is how we can copy all the elements of one array to another array using map(_:) method, = operator, and append(contentsOf:) method. Because Swift does not provide a library function to copy the elements of the array just like Java. Java has an in-built library function named arraycopy() to copy the array elements.