C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Combined: The combined array has 6 elements. This is equal to the length of the two arrays "numbers" and "numbers2."
Swift program that combines two arrays
let numbers = [10, 20, 30]
let numbers2 = [1, 2, 3]
// Combine the two arrays into a third array.
// ... The first 2 arrays are not changed.
let combined = numbers + numbers2
print(combined)
Output
[10, 20, 30, 1, 2, 3]
Swift program that appends multiple elements
// This must be a var.
var colors = ["red"]
// Add two elements to the array.
// ... The arrays are combined.
colors += ["orange", "blue"]
// Add more elements.
colors += ["silver", "gold"]
print(colors)
Output
["red", "orange", "blue", "silver", "gold"]
Swift program that uses append, contentsOf
var animals = ["cat"]
var animals2 = ["bird", "fish", "dog"]
// Add the second array to the first.
// ... This is like using the "add" operator.
animals.append(contentsOf: animals2)
print(animals)
Output
["cat", "bird", "fish", "dog"]