0
Guys i have a doubt
How to print array elements in array just array elements, whenever i print array like : let array = [1,2,3,5] print("Array elements are \(array)") Output: [1,2,3,5] My question why square brackets are also being displayed in output :/
4 Respuestas
+ 1
I don't understand the code, can u explain me what is "number" "terminator:" and why we used double quotes?
+ 1
Okay if you want to print the individual elements...,it is very easy.
Do this
let array = [1,2,3,5]
for number in array{
print("Array element: \(number)
This will print
Array element : 1
Array element : 2
..............
0
You can use for-loop to print the elements one by one
let arr = [ 1, 2, 3, 4, 5 ]
for number in arr
{
print(number, terminator : " ")
}
0
<number> is just a variable used in the for-loop. Its content changes with every loop iteration, it will contain each array element, one at a time, from the first element to the last. In simple terms it can be understood as "for each <number> (as element) in array <arr>".
About the 'terminator', and the double quotes, you may understand better by reading this 👇
https://www.programiz.com/swift-programming/basic-input-output