如何将元素从一个切片移动到另一个切片?

huangapple go评论66阅读模式
英文:

How to move elements from one slice to another

问题

package main

import (
	"fmt"
)

func main() {
	arr0 := []int{
		1,2,3,4,5,
	}
	arr1 := []int{}

	fmt.Println(arr0)
	fmt.Println(arr1)
	fmt.Println("转移中...")
	transfer(&arr0, &arr1)
	fmt.Println(arr0)
	fmt.Println(arr1)
}

func transfer(arr0 *[]int, arr1 *[]int) {
	tmp := make([]int, 0)
	for i:=0;i<len(*arr0);i++ {
		tmp = append(tmp, (*arr0)[i])
	}

	*arr1 = tmp
	s := make([]int, 0)
	*arr0 = s
}

对于transfer函数,我打算将切片arr0的元素转移到切片arr1中,并清空切片arr0

但是这个操作没有成功

这是我的输出结果

[1 2 3 4 5]
[]
转移中...
[1 2 3 4 5]
[]

转移后,我需要以下结果。
[]
[1 2 3 4 5]
但实际上,main函数中的arr0和arr1保持不变!

有人能告诉我为什么不行吗?

我以为在内存中应该是这样的

[![enter image description here][1]][1]

运行transfer函数后

[![enter image description here][2]][2]


<details>
<summary>英文:</summary>

package main

import (
"fmt"
)

func main() {
arr0 := []int{
1,2,3,4,5,
}
arr1 := []int{}

fmt.Println(arr0)
fmt.Println(arr1)
fmt.Println(&quot;transferring...&quot;)
transfer(&amp;arr0, &amp;arr1)
fmt.Println(arr0)
fmt.Println(arr1)

}

func transfer(arr0 *[]int, arr1 *[]int) {
tmp := make([]int, 0)
for i:=0;i<len(*arr0);i++ {
tmp = append(tmp, (*arr0)[i])
}

arr1 = &amp;tmp
s := make([]int, 0)
arr0 = &amp;s

}


For function of transfer, I intented to transfer elements of slice arr0 to slice arr1 and empty slice arr0

But it is not successful

Here is my output

[1 2 3 4 5]
[]
transferring...
[1 2 3 4 5]
[]


After transferring, I need the result below. 
[]
[1 2 3 4 5]
But actually, arr0, and arr1 in the main function remain as it was!

can someone tell me why this is not ok?

I thought in the memory, it should be like this

[![enter image description here][1]][1]

after running transfer function

[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/WUBOZ.png
  [2]: https://i.stack.imgur.com/e2TZa.png

</details>


# 答案1
**得分**: 3

@jacobsa给出了一个很好的答案。

记住这一点。你可以以更好的性能实现相同的效果。Golang为此提供了一个很好的机会。

```go
package main

import (
	"fmt"
)

func main() {
	arr0 := []int{
		1, 2, 3, 4, 5,
	}
	arr1 := []int{}

	fmt.Println(arr0)
	fmt.Println(arr1)
	fmt.Println("transferring...")
	transfer(&arr0, &arr1)
	fmt.Println(arr0)
	fmt.Println(arr1)
}

func transfer(arr0 *[]int, arr1 *[]int) {
	*arr0, *arr1 = *arr1, *arr0
}
英文:

@jacobsa has given an excellent answer.

Just keep that in mind. You can achieve the same effect but with better performance. Golang offers an excellent opportunity for this.

package main

import (
	&quot;fmt&quot;
)

func main() {
	arr0 := []int{
		1, 2, 3, 4, 5,
	}
	arr1 := []int{}

	fmt.Println(arr0)
	fmt.Println(arr1)
	fmt.Println(&quot;transferring...&quot;)
	transfer(&amp;arr0, &amp;arr1)
	fmt.Println(arr0)
	fmt.Println(arr1)
}

func transfer(arr0 *[]int, arr1 *[]int) {
	*arr0, *arr1 = *arr1, *arr0
}

答案2

得分: 2

这两行代码:

arr1 = &amp;tmp
arr0 = &amp;s

在函数内部改变了局部变量arr1arr0。这些变量恰好是指针,但它们只是main函数提供的输入指针的副本,它们不是指向输入指针的引用。

如果你改变了arr1arr0指针所指向的内容,而不是指针本身,那么你会看到main函数提供的值发生变化:

*arr1 = tmp
*arr0 = s
英文:

These two lines:

arr1 = &amp;tmp
arr0 = &amp;s

change the local variables arr1 and arr0 within the function. Those variables happen to be pointers, but they are just copies of the input pointers provided by main—they are not references to the input pointers.

If you changed the things the arr1 and arr0 pointers point to, rather than the pointers themselves, then you would see a change to the values provided by main:

*arr1 = tmp
*arr0 = s

huangapple
  • 本文由 发表于 2022年12月28日 17:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/74938295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定