Remove the element from array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • None

    Remove the element from array

    Hi,

    I have declared array as Int[] ids = new int[50];

    In ArrayList we can remove specified index using RemoveAt(5)
    method. My question is how can we do this one with int array (single
    dimensional array) or else is there any alternative solution for this?

    If anybody knows the solution pls let me know.

    Thanks and Regards,
    Vinothkumar B
    bvinoth@tvsinfo tech.com

  • Greg Young

    #2
    Re: Remove the element from array

    You can't remove an item from an array you could say set the position to
    null for an array of a reference type .. Arrays are a fixed size data
    structure ..

    when you define an array of int [50] think about it creating 50 ints one
    after another .. the array points to the first 1 ... so array[22] points to
    the memory address of array[0] + 22 * size of your data. In order to remove
    an index you would have to recreate the array

    Cheers,

    Greg Young
    MVP - C#
    "None" <vinkumrect@gma il.com> wrote in message
    news:1147239731 .443447.169190@ u72g2000cwu.goo glegroups.com.. .[color=blue]
    > Hi,
    >
    > I have declared array as Int[] ids = new int[50];
    >
    > In ArrayList we can remove specified index using RemoveAt(5)
    > method. My question is how can we do this one with int array (single
    > dimensional array) or else is there any alternative solution for this?
    >
    > If anybody knows the solution pls let me know.
    >
    > Thanks and Regards,
    > Vinothkumar B
    > bvinoth@tvsinfo tech.com
    >[/color]


    Comment

    • V

      #3
      Re: Remove the element from array

      You could of course write a wrapper over your array which would move
      each of the items below the deleted item one step up.

      This would be VERY ineffcient of course, but depends on your
      requirements.

      Regards,
      Vaibhav

      Comment

      • Altaf Al-Amin Najwani

        #4
        RE: Remove the element from array

        Interesting ... Microsoft Implements ArrayList as internal array of object
        references to your value or referene types. As ArrayList maintains references
        so removeat(5) means that object reference will be done null and that
        particular element is finalized and references are refreshed that is
        arraylist[i] references to arraylist[i+1]. Its very easy to do if array is
        object array ... but if you are creating an array of like int or string (any
        value type ) then its very tedious.

        "None" wrote:
        [color=blue]
        > Hi,
        >
        > I have declared array as Int[] ids = new int[50];
        >
        > In ArrayList we can remove specified index using RemoveAt(5)
        > method. My question is how can we do this one with int array (single
        > dimensional array) or else is there any alternative solution for this?
        >
        > If anybody knows the solution pls let me know.
        >
        > Thanks and Regards,
        > Vinothkumar B
        > bvinoth@tvsinfo tech.com
        >
        >[/color]

        Comment

        • Nick Hounsome

          #5
          Re: Remove the element from array


          "None" <vinkumrect@gma il.com> wrote in message
          news:1147239731 .443447.169190@ u72g2000cwu.goo glegroups.com.. .[color=blue]
          > Hi,
          >
          > I have declared array as Int[] ids = new int[50];
          >
          > In ArrayList we can remove specified index using RemoveAt(5)
          > method. My question is how can we do this one with int array (single
          > dimensional array) or else is there any alternative solution for this?[/color]

          If arrays could do everything ArrayList does then there would be no need for
          ArrayList.

          Removing stuff from an array is never going to be efficient however you do
          it so just use ArrayList and ArrayList.ToArr ay


          Comment

          Working...