CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    4

    How can I delete multiple items from collection?

    Hello,

    How do I delete multiple elements from collection (array) at once?

    I'd like to do that by index.

    For example, I want to delete elements with indexes 1, 2, 3 and 4.

    If I delete element at index 1, all elements and their indexes will be shifted. That's the problem. I need to delete the elements in compact index section, like: 1,2,3,4 or 7,8,9,10.


    Thank you in advance

    Goran

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: How can I delete multiple items from collection?

    If this is an ArrayList you have a RemoveRange method which takes the start index and the number of elements to remove.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    May 2003
    Location
    Germany
    Posts
    936

    Re: How can I delete multiple items from collection?

    A quick and dirty hack is to delete from the highest to the lowest index. So you will not getting in trouble with shifted indexes. Therefore you have to sort your indexes for removing in the right way.
    For solving this problem in a good way you should use an ArrayList and the Remove()-method. So you do not get in trouble with any index.
    Useful or not? Rate my posting. Thanks.

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: How can I delete multiple items from collection?

    Quote Originally Posted by torrud
    A quick and dirty hack is to delete from the highest to the lowest index. So you will not getting in trouble with shifted indexes. Therefore you have to sort your indexes for removing in the right way.
    For solving this problem in a good way you should use an ArrayList and the Remove()-method. So you do not get in trouble with any index.
    Try as torrud,darwen tol you you can do by that way .i mean to say just enter the index store all the index no in a array make a loop and use remove at and specify index no in it.

    Second if you want to do it in simple array only

    do same thing except after removing 1 element make a left shift to array element and then again perform the calculation.

    it's apposite of insertion sort method forget sort for a while time
    but you can get a idea by this that how it take place same way you can delete also

    if still problem let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured