英文:
transform item value in array/slice
问题
我想创建一个新的数组,其中每个值都是原始数组中对应值的两倍,我的代码有什么问题?
func main() {
myArray := []string{"1", "2", "3"}
var newArray = []string
for i, arr := range myArray {
newArray = newArray[i] * 2
}
fmt.Println(newArray)
}
你的代码有几个问题:
- 在声明
newArray
变量时,你应该使用[]string{}
来初始化一个空的字符串数组。 - 在循环中,你应该使用
append()
函数将新的元素添加到newArray
中,而不是直接赋值给索引位置。 - 在乘以2之前,你需要将字符串转换为整数类型。
下面是修复后的代码:
import (
"fmt"
"strconv"
)
func main() {
myArray := []string{"1", "2", "3"}
var newArray = []string{}
for _, arr := range myArray {
num, _ := strconv.Atoi(arr)
newNum := strconv.Itoa(num * 2)
newArray = append(newArray, newNum)
}
fmt.Println(newArray)
}
这样,你就可以得到一个新的数组,其中每个值都是原始数组中对应值的两倍。
英文:
I want to create a new array where each value is 2 times the corresponding value in the original array, what's wrong with my code?
func main() {
myArray := []string{"1", "2", "3"}
var newArray = []string
for i, arr := range myArray {
newArray = newArray[i] * 2
}
fmt.Println(newArray)
}
答案1
得分: 3
- 使用整数切片而不是字符串切片。例如,
[]string{"1", "2", "3"}
->[]int{1, 2, 3}
。 - 类型不是值。例如,
var newArray = []string
是一个无效的语句。要初始化一个空切片,省略=
运算符,或使用复合类型字面量,或者在这种情况下更好地使用make
函数并指定myArray
的长度。 - 未使用的块变量在 Go 中是非法的。例如,在
for i, arr := myArray {
中的arr
在循环中未被使用,因此会导致编译时错误。 - 不能将字符串与整数相乘。例如,如果
newArray
的类型是[]string
,则newArray[i] * 2
是非法的,会导致编译时错误。 - 不能更改变量的类型。例如,如果
newArray
的类型是[]int
,则语句newArray = newArray[i] * 2
是非法的,因为newArray[i] * 2
的结果是int
类型,而不能将int
赋值给类型为[]int
的变量。
func main() {
myArray := []int{1, 2, 3}
newArray := make([]int, len(myArray))
for i := range myArray {
newArray[i] = myArray[i] * 2
}
fmt.Println(newArray)
}
链接:https://play.golang.org/p/Tc0-51GiAhS
英文:
- Use an integer slice instead of a string slice. i.e.
[]string{"1", "2", "3"}
->[]int{1, 2, 3}
. - Types are not values. i.e.
var newArray = []string
is an invalid statement. To initialize an empty slice omit the=
operator, or use a composite type literal, or even better in this case, usemake
with the length ofmyArray
. - Unused block variables are illegal in Go. i.e. The
arr
infor i, arr := myArray {
is not used within the loop and will therefore cause a compile time error. - You cannot multiply a string with an integer. i.e.
newArray[i] * 2
wherenewArray
is of type[]string
is illegal and will cause compile time error. - You cannot change the type of a variable. i.e.
newArray = newArray[i] * 2
ifnewArray
is of type[]int
the statement is illegal becausenewArray[i] * 2
would evaluate toint
and you cannot assignint
to a variable of type[]int
.
func main() {
myArray := []int{1, 2, 3}
newArray := make([]int, len(myArray))
for i := range myArray {
newArray[i] = myArray[i] * 2
}
fmt.Println(newArray)
}
答案2
得分: 1
你的语法有几个问题。
你声明数组的方式不正确(第一个声明将myArray声明为切片,第二个声明无效且无法编译),而且你试图对字符串进行乘法运算。
你声明了arr但没有使用它,这也会导致编译错误。
以下代码片段可以实现你想要的整数操作:
myArray := [3]int{1, 2, 3}
newArray := myArray // 将原始数组复制到一个新数组
for i := range myArray { // 只关心索引,不关心值
newArray[i] *= 2 // 在新数组中将值翻倍
}
fmt.Println(newArray)
另一种方法是在不先复制原始数组的情况下实现:
myArray := [3]int{1, 2, 3}
newArray := [3]int{}
for i := range myArray {
newArray[i] = myArray[i] * 2
}
fmt.Println(newArray)
英文:
There's quite a few things wrong with your syntax.
You are declaring your arrays wrong ( your first one declares a myArray as a slice, your second one is invalid and won't compile ), and you are trying to multiply strings.
You are declaring arr but not using it which will also give a compile error.
This snippet will do what you want with ints:
myArray := [3]int{1, 2, 3}
newArray := myArray // copies the original array to a new one
for i := range myArray { // don't care about the value, only the index
newArray[i] *= 2 // double the values in the new array
}
fmt.Println(newArray)
Another alternative without first copying the original array:
myArray := [3]int{1, 2, 3}
newArray := [3]int{}
for i := range myArray {
newArray[i] = myArray[i] * 2
}
fmt.Println(newArray)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论