
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
swift Array capacity Property
The capacity property of the array is used to check the capacity of the given array. Here the capacity represents the total number of elements an array can hold without allocating new memory. The capacity of the array can be larger than the count/length of the array. This additional capacity enables us to perform append operations without reallocating the storage every time an element is added.
For example, we have an array = [12, 33, 56, 1, 66, 3]. Now using the capacity property we will find the capacity of the array that is 6.
Syntax
Following is the syntax for capacity property.
var capacity : Int {get}
Return value
This function returns the capacity of the array.
Now we will discuss the use of the capacity property with the help of the following examples:
Example 1
Swift program to find the capacity of the array with the help of capacity property.
import Foundation // Array of string type let language = ["Swift", "Python", "Java", "CSharp"] // Finding the capacity of the array let result1 = language.capacity print("The capacity of the array is : \(result1)") // Array of integer type let number = [34, 5, 6, 3, 3, 22, 12, 1] // Finding the capacity of the array let result2 = number.capacity print("The capacity of the array is : \(result2)")
Output
The capacity of the array is : 4 The capacity of the array is : 8
Example 2
Swift program to check the dynamic growth of the capacity. Here we will check the capacity of the array after appending the new elements.
import Foundation var myArr = [3, 4, 3, 2] let initalCapacity = myArr.capacity print("Initial capacity of the array is:", initalCapacity) myArr.append(3) myArr.append(23) myArr.append(12) let newCapacity = myArr.capacity print("Capacity after appending elements: ", newCapacity)
Output
Initial capacity of the array is: 4 Capacity after appending elements: 9
Example 3
Swift program to find the capacity of the array after removing 5 elements from it. Here we will check the capacity of the array after removing 5 elements. So the after removing 5 elements the capacity of the array is same.
import Foundation var myArr = [33, 44, 13, 12, 34, 56, 77, 8, 45, 2, 3] let initalCapacity = myArr.capacity print("Initial capacity of the array is:", initalCapacity) // Removing five elements myArr.remove(at: 1) myArr.remove(at: 2) myArr.remove(at: 3) myArr.remove(at: 4) myArr.remove(at: 5) let newCapacity = myArr.capacity print("Capacity after removing 5 elements: ", newCapacity)
Output
Initial capacity of the array is: 11 Capacity after removing 5 elements: 11