FAQ
Hi,

I am trying to iterate a list and remove some elements, as followed codes:

objlist := list.New()

for i:=0; i<1000; i++ {

j := i + 5

objlist.PushBack(j)

}

fmt.Println("objlist length: ", objlist.Len())

for e:=objlist.Front(); e!=nil; e=e.Next(){

fmt.Println(e.Value.(int))

if e.Value.(int) % 5 == 0 {

fmt.Println(e.Value)

objlist.Remove(e)

}

}


but it seems every time as soon as the objlist.Remove() is executed, it
will break out of the for loop..I have no idea why it is. Can anyone give
some hint?

Thanks.



--

Search Discussions

  • Robert Sandra at Oct 2, 2012 at 1:53 am
    find the following example code in list_test.go, it works well. seems a
    little tricky..

    var next *Element

    for e := l.Front(); e != nil; e = next {

    next = e.Next()

    l.Remove(e)

    }
    On Monday, October 1, 2012 9:28:58 PM UTC-4, Robert Sandra wrote:

    Hi,

    I am trying to iterate a list and remove some elements, as followed codes:

    objlist := list.New()

    for i:=0; i<1000; i++ {

    j := i + 5

    objlist.PushBack(j)

    }

    fmt.Println("objlist length: ", objlist.Len())

    for e:=objlist.Front(); e!=nil; e=e.Next(){

    fmt.Println(e.Value.(int))

    if e.Value.(int) % 5 == 0 {

    fmt.Println(e.Value)

    objlist.Remove(e)

    }

    }


    but it seems every time as soon as the objlist.Remove() is executed, it
    will break out of the for loop..I have no idea why it is. Can anyone give
    some hint?

    Thanks.

    --
  • Si guy at Oct 2, 2012 at 1:56 am
    If you remove e from the list, then the call to e.Next in the next loop will return nil. You have to either reassign e and restart the loop manually, or defer the removal until after the loop is finished, or store e and remove it in the next iteration. There are other ways as well, try googling "remove element from doubly linked list in for loop" or something similar.

    As a side note, you might want to use a slice instead of a list unless there are specific reasons for a list.

    --
  • Robert Sandra at Oct 2, 2012 at 2:03 am
    is that because slice more efficient than list? if there is a large number
    of objects need to be added to the slice/list, which one will be more
    efficient? since as far as I know, slice need dynamically increase its
    size..
    On Monday, October 1, 2012 9:56:50 PM UTC-4, si guy wrote:

    If you remove e from the list, then the call to e.Next in the next loop
    will return nil. You have to either reassign e and restart the loop
    manually, or defer the removal until after the loop is finished, or store e
    and remove it in the next iteration. There are other ways as well, try
    googling "remove element from doubly linked list in for loop" or something
    similar.

    As a side note, you might want to use a slice instead of a list unless
    there are specific reasons for a list.
    --
  • Si guy at Oct 2, 2012 at 2:42 am

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 2, '12 at 1:29a
activeOct 2, '12 at 2:42a
posts5
users2
websitegolang.org

2 users in discussion

Robert Sandra: 3 posts Si guy: 2 posts

People

Translate

site design / logo © 2021 Grokbase