Say, I have an array [20, 2, 3]
How can I multiply each Int value of this array in Swift?
So 2 x array becomes [40, 4, 6]
, 3 x array becomes [60, 6, 9]
and so on?
说,我有一个数组[20,2,3]如何在Swift中乘以这个数组的每个Int值?那么2 x数组变为[40,4,6],3 x数组变为[60,6,9]等等?
3 个解决方案
#1
11
You can use .map()
:
你可以使用.map():
let values = [20, 2, 3]
let doubles = values.map { $0 * 2 }
let triples = values.map { $0 * 3 }
If you want to do the update in-place:
如果要进行就地更新:
var values = [20, 2, 3]
values.enumerate().forEach { index, value in
values[index] = value * 2
}
// values is now [40, 4, 6]
#2
4
map
is definitely a good way to go – although if you want to get the syntax that you asked for in your question "2 x array becomes ...", you could overload the *
operator.
map肯定是一个很好的方法 - 虽然如果你想在你的问题“2 x array变成......”中得到你要求的语法,你可以重载*运算符。
This will allow you to multiply an array by a scalar inline, if the array's elements are multipliable and of the same type as the scalar.
如果数组的元素是可乘的且与标量相同的类型,这将允许您将数组乘以标量内联。
protocol Multipliable {
func *(lhs:Self, rhs:Self) -> Self
}
// add any additional types that you want to be multipliable here
extension Int:Multipliable {}
extension Double:Multipliable {}
extension Float:Multipliable {}
// map through the array elements, multiplying each by the rhs argument
func *<T:Multipliable>(lhs:[T], rhs:T) -> [T] {
return lhs.map {$0 * rhs}
}
// use an inout argument in order to multiply an array in-place
func *=<T:Multipliable>(inout lhs:[T], rhs:T) {
lhs = lhs*rhs
}
You can then use it like this:
然后你可以像这样使用它:
let yourArray = [2, 3, 4]
var arr = yourArray * 4 // arr = [8, 12, 16]
arr *= 3 // arr = [24, 36, 48]
#3
1
You can use a for loop.
你可以使用for循环。
var array = [20, 2, 3]
let multiplier = 2
for i in 0...array.count - 1{
array[i] = array[i] * multiplier
}
#1
11
You can use .map()
:
你可以使用.map():
let values = [20, 2, 3]
let doubles = values.map { $0 * 2 }
let triples = values.map { $0 * 3 }
If you want to do the update in-place:
如果要进行就地更新:
var values = [20, 2, 3]
values.enumerate().forEach { index, value in
values[index] = value * 2
}
// values is now [40, 4, 6]
#2
4
map
is definitely a good way to go – although if you want to get the syntax that you asked for in your question "2 x array becomes ...", you could overload the *
operator.
map肯定是一个很好的方法 - 虽然如果你想在你的问题“2 x array变成......”中得到你要求的语法,你可以重载*运算符。
This will allow you to multiply an array by a scalar inline, if the array's elements are multipliable and of the same type as the scalar.
如果数组的元素是可乘的且与标量相同的类型,这将允许您将数组乘以标量内联。
protocol Multipliable {
func *(lhs:Self, rhs:Self) -> Self
}
// add any additional types that you want to be multipliable here
extension Int:Multipliable {}
extension Double:Multipliable {}
extension Float:Multipliable {}
// map through the array elements, multiplying each by the rhs argument
func *<T:Multipliable>(lhs:[T], rhs:T) -> [T] {
return lhs.map {$0 * rhs}
}
// use an inout argument in order to multiply an array in-place
func *=<T:Multipliable>(inout lhs:[T], rhs:T) {
lhs = lhs*rhs
}
You can then use it like this:
然后你可以像这样使用它:
let yourArray = [2, 3, 4]
var arr = yourArray * 4 // arr = [8, 12, 16]
arr *= 3 // arr = [24, 36, 48]
#3
1
You can use a for loop.
你可以使用for循环。
var array = [20, 2, 3]
let multiplier = 2
for i in 0...array.count - 1{
array[i] = array[i] * multiplier
}