simple question about arrays

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

    #1

    simple question about arrays

    Dear all,
    I am wondering, that I can't find how to delete an element from an array.
    dim a as string()
    a=SomeString.sp lit(",")

    for ...
    if _string="" then a.delete????
    ...


  • Herfried K. Wagner [MVP]

    #2
    Re: simple question about arrays

    "Boni" <oilia@nospam > schrieb:[color=blue]
    > I am wondering, that I can't find how to delete an element from an array.
    > dim a as string()
    > a=SomeString.sp lit(",")
    >
    > for ...
    > if _string="" then a.delete????[/color]

    You'll have to create a new array of appropriate size and copy over the
    items of the original array to the new array using 'Array.Copy' or
    'Array.CopyTo'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Boni

      #3
      Re: simple question about arrays

      It is ugly. Is there no better solution? If split(.) returns an array I
      can't take other collection type. Is it really nothing more appropriate?
      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schrieb im Newsbeitrag
      news:%23OoWmJfj FHA.3568@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > "Boni" <oilia@nospam > schrieb:[color=green]
      >> I am wondering, that I can't find how to delete an element from an array.
      >> dim a as string()
      >> a=SomeString.sp lit(",")
      >>
      >> for ...
      >> if _string="" then a.delete????[/color]
      >
      > You'll have to create a new array of appropriate size and copy over the
      > items of the original array to the new array using 'Array.Copy' or
      > 'Array.CopyTo'.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]


      Comment

      • James Randle of Epidermis

        #4
        RE: simple question about arrays

        You can't 'delete' an element from the middle of an array. You need to use a
        collection for that functionality, or run through your array, copying all non
        blank elements into another array.

        "Boni" wrote:
        [color=blue]
        > Dear all,
        > I am wondering, that I can't find how to delete an element from an array.
        > dim a as string()
        > a=SomeString.sp lit(",")
        >
        > for ...
        > if _string="" then a.delete????
        > ...
        >
        >
        >[/color]

        Comment

        • Armin Zingler

          #5
          Re: simple question about arrays

          "Boni" <oilia@nospam > schrieb[color=blue]
          > It is ugly. Is there no better solution? If split(.) returns an
          > array I can't take other collection type. Is it really nothing more
          > appropriate?[/color]


          In addition the other answers: You can't delete an item because you would
          have to quarry out a piece of your RAM module. Sounds funny but that's how
          it is. Just like you can't delete a line from a text file on your HD. You
          have to overwrite it by the subsequent lines (or array elements). As the
          length of an array is fixed, there would be one remaining ununsed item at
          the end. To solve the problem, the whole array must be copied into a new
          array - without the item to be deleted. The same is done by Redim Preserve
          BTW.


          Armin

          Comment

          • Phill.  W

            #6
            Re: simple question about arrays

            "Boni" <oilia@nospam > wrote in message
            news:%23vvPLDfj FHA.2156@TK2MSF TNGP14.phx.gbl. ..[color=blue]
            > Dear all,
            > I am wondering, that I can't find how to delete an element from an array.[/color]

            I'd suggest constucting an ArrayList from the String array :

            Dim a As New ArrayList( SomeString.Spli t(","c) )

            For iIdx as Integer = a.Count - 1 To 0 Step -1
            If False Then
            a.RemoveAt( iIdx )
            End if
            Next

            HTH,
            Phill W.


            Comment

            • Chris

              #7
              Re: simple question about arrays

              Boni wrote:[color=blue]
              > Dear all,
              > I am wondering, that I can't find how to delete an element from an array.
              > dim a as string()
              > a=SomeString.sp lit(",")
              >
              > for ...
              > if _string="" then a.delete????
              > ..
              >
              >[/color]

              There is another option, you can use arraylist to do the removing for you.

              dim Str() as string
              dim Arr as new ArrayList(str.s plit(","))

              for ii as integer = 0 to Arr.Count-1
              if cstr(Arr(ii)).l ength = 0 then Arr.Remove(ii)
              Next

              Chris

              Comment

              • Dennis

                #8
                RE: simple question about arrays

                I think you would find it quicker to dim another array the same size of array
                "a" then copy only the non blank elements to the new array and count them.
                Then redim preserve the new array to the count of non items, i.e.

                dim b(a.upperbound) as string

                dim count as integer =0
                for i as integer= 0 to a.upperbound
                if a(i) <>"" then
                b(count)=a(i)
                count = count+1
                end if
                next

                redim preserve b(count-1)
                --
                Dennis in Houston


                "Boni" wrote:
                [color=blue]
                > Dear all,
                > I am wondering, that I can't find how to delete an element from an array.
                > dim a as string()
                > a=SomeString.sp lit(",")
                >
                > for ...
                > if _string="" then a.delete????
                > ...
                >
                >
                >[/color]

                Comment

                • Boni

                  #9
                  Re: simple question about arrays


                  [color=blue]
                  > In addition the other answers: You can't delete an item because you would
                  > have to quarry out a piece of your RAM module[/color]
                  Was it a joke? I did it and my PC is dead now! :)


                  Comment

                  • Boni

                    #10
                    Re: simple question about arrays

                    Thank you all. ArrayList is what I was looking for:)



                    "Chris" <no@spam.com> schrieb im Newsbeitrag
                    news:ePou4VgjFH A.3972@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > Boni wrote:[color=green]
                    >> Dear all,
                    >> I am wondering, that I can't find how to delete an element from an array.
                    >> dim a as string()
                    >> a=SomeString.sp lit(",")
                    >>
                    >> for ...
                    >> if _string="" then a.delete????
                    >> ..
                    >>
                    >>[/color]
                    >
                    > There is another option, you can use arraylist to do the removing for you.
                    >
                    > dim Str() as string
                    > dim Arr as new ArrayList(str.s plit(","))
                    >
                    > for ii as integer = 0 to Arr.Count-1
                    > if cstr(Arr(ii)).l ength = 0 then Arr.Remove(ii)
                    > Next
                    >
                    > Chris[/color]


                    Comment

                    • Armin Zingler

                      #11
                      Re: simple question about arrays

                      "Boni" <oilia@nospam > schrieb[color=blue]
                      >
                      >[color=green]
                      > > In addition the other answers: You can't delete an item because
                      > > you would have to quarry out a piece of your RAM module[/color]
                      > Was it a joke? I did it and my PC is dead now! :)[/color]


                      Sorry, forgot the smiley. ;-)

                      Armin

                      Comment

                      Working...