Golang:覆盖切片的初始元素

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

Golang: override initial elements of a slice

问题

我想初始化一个长度为3的空整数切片,将随机整数存储在切片中,并在每次迭代时对切片进行排序。以下代码实现了这一目标:

package main

import (
	"fmt"
	"math/rand"
	"sort"
)

func main() {
	myslice := make([]int, 3)
	for i := 0; i < 10; i++ {
		myslice = append(myslice, rand.Intn(100))
		sort.IntSlice(myslice).Sort()
		fmt.Println(myslice)
	
	}
}

最终结果:[0 0 0 5 23 28 43 45 50 57 68 76 87] 但是,在追加新元素之前,我还想覆盖初始元素(也就是去掉那些零)。由于切片在每次迭代时都是排序的,如果 i < 3,调用 myslice[i] = rand.Intn(100) 并不一定有效。

我的初始解决方案是编写一个函数来获取 i < 3 的第一个零值的索引,并用新的随机值替换该索引... 但是,我想知道是否有更好的方法来覆盖切片的初始值。

英文:

I want to initialize an empty integer slice of length 3, store random ints in the slice, and sort the slice at each iteration. This code accomplishes that:

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;sort&quot;
)

func main() {
	myslice := make([]int, 3)
	for i := 0; i &lt; 10; i++ {
		myslice = append(myslice, rand.Intn(100))
		sort.IntSlice(myslice).Sort()
		fmt.Println(myslice)
	
	}
}

End result: [0 0 0 5 23 28 43 45 50 57 68 76 87] However, I would also like to override the initial elements (in other words, get rid of those zeroes) before appending new elements. Since the slice is sorted at each iteration, calling myslice[i] = rand.Intn(100) if i &lt; 3 wouldn't necessarily work.

My initial solution is to write a function to get the first index of zero for i &lt; 3, and replace that index with a new random value... however, I was wondering whether there is a better option to override the initial values of a slice.

答案1

得分: 5

使用以下代码创建一个新的[]int切片,长度为0,容量为3:

myslice := make([]int, 0, 3)

尽管你要添加的元素超过了3个,但这样做实际上没有太多意义;如果你要预先分配内存,就预先分配整个切片;如果你不打算预先分配整个切片,只需使用var myslice []int即可。

这个问题在《Go 语言之旅》中有详细介绍,我强烈建议你去完成一下。这只需要几分钟时间,涵盖了所有的语言基础知识。

英文:

Use:

myslice := make([]int, 0, 3)

to create a new []int with length 0 and capacity 3. Though since you're appending more than 3 elements, this is fairly pointless; if you're going to preallocate, preallocate the whole thing, and if you're not going to preallocate the whole thing, just use var myslice []int.

This is covered in the Tour of Go, which I highly recommend going through. It only takes a few minutes and covers all the language basics.

答案2

得分: 2

根据Adrian的建议,将代码修改如下:

package main

import (
	"fmt"
	"math/rand"
	"sort"
)

func main() {
	var myslice []int
	for i := 0; i < 10; i++ {
		myslice = append(myslice, rand.Intn(100))
		sort.IntSlice(myslice).Sort()
		fmt.Println(myslice)

	}
}

输出结果:

[81]
[81 87]
[47 81 87]
[47 59 81 87]
[47 59 81 81 87]
[18 47 59 81 81 87]
[18 25 47 59 81 81 87]
[18 25 40 47 59 81 81 87]
[18 25 40 47 56 59 81 81 87]
[0 18 25 40 47 56 59 81 81 87]
英文:

Based on the suggestion of Adrian ,modifying the code as follows:

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;sort&quot;
)

func main() {
	var myslice []int
	for i := 0; i &lt; 10; i++ {
		myslice = append(myslice, rand.Intn(100))
		sort.IntSlice(myslice).Sort()
		fmt.Println(myslice)

	}
}

Output:

[81]
[81 87]
[47 81 87]
[47 59 81 87]
[47 59 81 81 87]
[18 47 59 81 81 87]
[18 25 47 59 81 81 87]
[18 25 40 47 59 81 81 87]
[18 25 40 47 56 59 81 81 87]
[0 18 25 40 47 56 59 81 81 87]

huangapple
  • 本文由 发表于 2021年8月10日 22:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68729057.html
匿名

发表评论

匿名网友

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

确定