golang list.remove
发布时间:2020-12-16 18:51:09 所属栏目:大数据 来源:网络整理
导读:for循环删除的时候要用临时变量保存e.Next(),删除后再赋值给e,否则删除后e=nil就直接退出了 func TestList(t *testing.T) {l := list.New()for i := 0; i 10; i++ {l.PushBack(i)}//fmt.Printf("%v",l.)for e := l.Front(); e != nil; e = e.Next() {fmt.P
for循环删除的时候要用临时变量保存e.Next(),删除后再赋值给e,否则删除后e=nil就直接退出了
func TestList(t *testing.T) { l := list.New() for i := 0; i < 10; i++ { l.PushBack(i) } //fmt.Printf("%v",l.) for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value.(int)) } fmt.Println("") var next *list.Element for e := l.Front(); e != nil; { if e.Value.(int) == 5 || e.Value.(int) == 7 { next = e.Next() l.Remove(e) e = next } else { e = e.Next() } } for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value.(int)) } t.Errorf("%v",l) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |