Open In App

Swift – How to Store Values in Arrays?

Improve
Improve
Like Article
Like
Save
Share
Report

An array is a collection of elements having a similar data type. Normally to store a value of a particular data type we use variables. Now suppose we want to store marks of 5 students of a class. We can create 5 variables and assign marks to each of them. But for a class having 50 or more students, creating these many variables doesn’t look good. Moreover, this practice should be avoided as it makes our source code less readable. To store a large number of elements of similar data types, we can use arrays. Elements can be accessed using index numbers in an array. In Swift, indexing starts with 0. In this article, we will see different methods to store values in an array using Swift programming language.

By Initializing an Array

We can store elements in the array directly at the time of initialization. 

Syntax:

var myArray : [data_type] = [val1, val2, val3,…]

Here, myArray is the array we want to create, data_type is the data type of elements, and val1, val2,.. are the elements we want to store in the array 

Example 1:

Swift




// Swift program to store value in the array 
  
// Initializing an array of integer type
var Array1 : [Int] = [1, 3, 5, 7]
  
// Initializing another array of character type
var Array2 : [Character] = ["G", "e", "e", "k"]
  
// Print arrays
print("Array1: \(Array1)")
print("Array2: \(Array2)")


Output:

Array1: [1, 3, 5, 7]
Array2: ["G", "e", "e", "k"]

Example 2:

Swift




// Swift program to store value in the array 
  
// Initializing an array of float type
var Array1 : [Float] = [1.2, 5.32, 5.56, 7.15]
  
// Initializing another array of String type
var Array2 : [String] = ["Geeks", "for", "Geeks"]
  
// Print arrays
print("Array1: \(Array1)")
print("Array2: \(Array2)")


Output:

Array1: [1.2, 5.32, 5.56, 7.15]
Array2: ["Geeks", "for", "Geeks"]

Using append() method

We can also initialize the array using the append() method. This method is used to add an element at the end of the given array.

Syntax:

myArray.append(value)

Here, myArray is the array that we have created and value is the element we want to add at the end of the array

Example 1:

Swift




// Swift program to store value in the array 
  
// Initializing an array of integer type
var Array1 : [Int] = [1, 3]
  
// Appending element at the end of the array
Array1.append(5)
Array1.append(7)
  
// Initializing another array of character type
var Array2 : [Character] = ["G", "e"]
  
// Appending element at the end of the array
Array2.append("e")
Array2.append("k")
  
// Print arrays
print("Array1: \(Array1)")
print("Array2: \(Array2)")


Output:

Array1: [1, 3, 5, 7]
Array2: ["G", "e", "e", "k"]

Example 2:

Swift




// Swift program to store value in the array 
  
// Initializing an array of float type
var Array1 : [Float] = [1.2, 5.32, 5.56]
  
print("Array 1 is: \(Array1)")
  
// Initializing another array of String type
var Array2 : [Float] = [2.9, 4.5, 2.0]
  
print("Array 2 is: \(Array2)")
  
// Appending all the elements 
// at the end of the array
Array2.append(contentsOf:Array1)
  
// Print arrays
print("Final Array: \(Array2)")


Output:

Array 1 is: [1.2, 5.32, 5.56]
Array 2 is: [2.9, 4.5, 2.0]
Final Array: [2.9, 4.5, 2.0, 1.2, 5.32, 5.56]

Using insert() method

We can also initialize the array using the insert() method. This method is used to add elements at a given position of the array.

Syntax:

myArray.insert(value, at : index)

Here, myArray is the array that we have created, value is the element we want to add and index is the index at which we want to add the element 

Example 1:

Swift




// Swift program to store value in the array 
  
// Initializing an array of integer type
var Array1 : [Int] = [1, 3]
  
// Inserting elements at an index
Array1.insert(5, at: 2)
Array1.insert(7, at: 3)
  
// Initializing another array of character type
var Array2 : [Character] = ["G", "e"]
  
// Inserting elements at an index
Array2.insert("e", at: 2)
Array2.insert("k", at: 3)
  
// Print arrays
print("Array1: \(Array1)")
print("Array2: \(Array2)")  


Output:

Array1: [1, 3, 5, 7]
Array2: ["G", "e", "e", "k"]

Example 2:

Swift




// Swift program to store value in the array 
  
// Initializing an array of float type
var Array1 : [Float] = [1.2, 5.32, 5.56]
  
// Inserting element at an index
Array1.insert(7.15, at: 3)
    
// Initializing another array of String type
var Array2 : [String] = ["Geeks", "for"]
  
// Inserting element at an index
Array2.insert("Geeks", at: 2)
  
 // Print arrays
print("Array1: \(Array1)")
print("Array2: \(Array2)")


Output:

Array1: [1.2, 5.32, 5.56, 7.15]
Array2: ["Geeks", "for", "Geeks"]


Last Updated : 23 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads