Swift program to create a two-dimensional array using the append() function

Here, we are going to learn how to create a two-dimensional array using the append() function in Swift programming language?
Submitted by Nidhi, on June 18, 2021

Problem Solution:

Here, we will create an empty 2D array and then create rows and added using the append() function into a created 2D array.

Program/Source Code:

The source code to create a two-dimensional array using the append() function is given below. The given program is compiled and executed successfully.

// Swift program to create a two-dimensional array 
// using append() function

import Swift

var twoDArr=[[Int]]()
var row1 = [Int]()

row1.append(1)
row1.append(3)
row1.append(5)

twoDArr.append(row1)

var row2 = [Int]()

row2.append(2)
row2.append(4)
row2.append(6)

twoDArr.append(row2)

print("Two Dimensional array: ",twoDArr)

Output:

Two Dimensional array:  [[1, 3, 5], [2, 4, 6]]

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created an empty 2D array twoDArr. Then we created two rows and added both rows into the created 2D array using the append() function and printed the result on the console screen.

Swift Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.