
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
Print a 2D Array in Swift
This article will teach us how to write a swift program to print a 2D array. Here we use the following methods ?
Using array name
Using nested for loop
Using subscript.
Using forEach() method.
Method 1: Print the 2D array using the array name
We can print a 2D array by directly using the name of the array in the print function.
Syntax
Following is the syntax ?
print(arrayName)
Here we simply pass the name of the array in the print function to display a 2D array.
Example
The following Swift program shows how to print 2D arrays using the name of the array.
import Foundation import Glibc // Creating a 2D array of string type var Array : [[String]] = [["Cow", "Dog", "Cat", "Monkey"], ["Sparrow", "Hawk", "Crow", "Swan"]] print("2D Array elements are:") // Printing the 2D array print(Array)
Output
2D Array elements are: [["Cow", "Dog", "Cat", "Monkey"], ["Sparrow", "Hawk", "Crow", "Swan"]]
Here in the above code, we create an array of string types, Now we directly use the name of the array in the print() function to display the 2D array on the output screen.
Method 2: Print 2D array using nested for loop
We can print a 2D array by using a nested for loop. Here we use two nested loops to iterate through each row and column of the 2D array.
Syntax
Following is the syntax of for loop ?
for variable in arrayName{ // Statement }
Here for the loop variable iterate through each element in the given arrayName.
Example
The following Swift program shows how to print a 2D array using nested for loop.
import Foundation import Glibc // Creating a 2D array of integer type var Array : [[Int]] = [[2, 4, 66, 42, 34], [23, 4, 5, 6]] print("2D Array elements are:") // Printing the 2D array using nested for loop for m in 0..<Array.count{ for n in 0..<Array[m].count{ print(Array[m][n]) } }
Output
2D Array elements are: 2 4 66 42 34 23 4 5 6
Here in the above code, we create an array of integer types. Now we use nested for loops to iterate through each element of the 2D array to print on the output screen.
Method 3: Print individual elements of a 2D array
To print a 2D array on the output screen we use subscripts. Subscript provides a shorthand syntax to access the individual elements of a 2D array.
Syntax
Following is the syntax? to display individual elements of the array ?
ArrayName[index][index]
Here we use [][] brackets to represent the position/index of the element in the array. Where the first bracket contains the index of the row and the second bracket contains the index of the column.
Example
The following Swift program shows how to print 2D arrays using subscripts.
import Foundation import Glibc // Creating a 2D array of string type var Array : [[String]] = [["Mohan", "Sohan"], ["Mukati", "Mona"]] print("2D Array elements are:") // Printing individual elements of the array print("[\(Array[0][0]), \(Array[0][1])]") print("[\(Array[1][0]), \(Array[1][1])]")
Output
2D Array elements are: [Mohan, Sohan] [Mukati, Mona]
Here in the above code, we create a 2D array of strings using nested brackets. Now we use subscripts to access the element according to the index value given inside the brackets [][]. Here Mohan is present at index [0][0], Sohan is present at index[0][1], and so on.
Method 4: Print 2D array using forEach() method
To print a 2D array we can also use the forEach() function. This function calls the specified closure on all the elements present in the given array. This method does not skip any element of the given array or we can say you can not get out from the forEach() method without processing all the elements of the given array.
Syntax
Following is the syntax of forEach method ?
func forEach(_closure:(self.element)throws ->Void)rethrows
Here closer take the elements of the given sequence as a parameter.
Example
The following Swift program shows how to print 2D arrays using the forEach() method.
import Foundation import Glibc // Creating a 2D array of integer type var Array : [[Int]] = [[2, 4, 5, 6], [6, 4, 2, 5]] print("2D Array elements are:") // Printing the 2D array using forEach() method Array.forEach{print($0)}
Output
2D Array elements are: [2, 4, 5, 6] [6, 4, 2, 5]
Here in the above code, we create a 2D array of an integer using nested brackets. Now we use the forEach() function to display the array. Here the iteration starts from index 0 because we use $0 if we use $1, then the index iteration starts from index 1.
Conclusion
So you can easily print a 2D array using either, the array name, nested for loop, subscript, or forEach() method. All the methods work pretty well with the 2D array so you can use them according to your requirement. So this is how you can print the 2D array.