ArrayList problem

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

    #1

    ArrayList problem

    Hi

    I've got an arraylist of integers.

    I want to be able to remove an integer from the list using a method
    like this:

    public void ArrayListRemove (int indexnum)
    {
    ArrayList.Remov e(ArrayList.Ind exOf(indexnum)) ;
    ArrayList.TrimT oSize();
    }

    It isn't working. I can't figure out why...

    I seem to be getting an Invalid Object reference error when it runs
    (that's what the debugger says, anyway!).

    Any advice will be greatly appreciated!

    Thanks.

    Jerry
  • Peter Koen

    #2
    Re: ArrayList problem

    Jerry S <jerry@philosop hers.co.uk> wrote in
    news:6k6opvg8d9 8nqqajlfk7984ok 37uogah0n@4ax.c om:
    [color=blue]
    > Hi
    >
    > I've got an arraylist of integers.
    >
    > I want to be able to remove an integer from the list using a method
    > like this:
    >
    > public void ArrayListRemove (int indexnum)
    > {
    > ArrayList.Remov e(ArrayList.Ind exOf(indexnum)) ;
    > ArrayList.TrimT oSize();
    > }
    >
    > It isn't working. I can't figure out why...
    >
    > I seem to be getting an Invalid Object reference error when it runs
    > (that's what the debugger says, anyway!).
    >
    > Any advice will be greatly appreciated!
    >
    > Thanks.
    >
    > Jerry
    >[/color]

    What's the name of your arrayList Object? in your code your are trying to
    call the Methods on the Classname.

    it should be more like this:

    public class myClass
    {
    ArrayList al = new ArrayList();

    public void ALRemove(int number)
    {
    al.Remove(al.In dexOf(number));
    }
    }

    and you shouldn't always call TrimToSize. That function needs quite some
    processing power. you should call it only when you removed many Items
    from the list. a good practice would be to call it when more than 10% of
    the items have been removed. with integers only in the list it would be
    enough to call it when you removed at least 100 items.

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • David

      #3
      Re: ArrayList problem

      In article <6k6opvg8d98nqq ajlfk7984ok37uo gah0n@4ax.com>, Jerry S wrote:[color=blue]
      > Hi
      >
      > I've got an arraylist of integers.
      >
      > I want to be able to remove an integer from the list using a method
      > like this:
      >
      > public void ArrayListRemove (int indexnum)
      > {
      > ArrayList.Remov e(ArrayList.Ind exOf(indexnum)) ;
      > ArrayList.TrimT oSize();
      > }[/color]

      Is indexnum the index of the number you want to remove, or the value of
      the number you want to remove? If indexnum is the value you want to
      remove, you probably want either..

      arrayList.Remov eAt(arrayList.I ndexOf(indexnum ))
      or
      arrayList.Remov e(indexnum)

      Obviously, the second one is simpler.

      Although if you've already determined the index number then

      arrayList.Remov eAt(indexnum)

      will do the job.
      [color=blue]
      >
      > It isn't working. I can't figure out why...
      >
      > I seem to be getting an Invalid Object reference error when it runs
      > (that's what the debugger says, anyway!).[/color]

      Well, that seems to point to deeper problems, and suggests that the
      ArrayList object has fallen out of scope someplace else in your code.

      BTW, are you really naming the object "ArrayList" ? It's incredibly
      confusing to give a class instance the same name as the class itself,
      IMHO.


      Comment

      • David

        #4
        Re: ArrayList problem

        In article <ertg$e$mDHA.17 40@TK2MSFTNGP12 .phx.gbl>, Peter Koen wrote:[color=blue]
        > ArrayList al = new ArrayList();
        >
        > public void ALRemove(int number)
        > {
        > al.Remove(al.In dexOf(number));
        > }[/color]

        Actually, no. That tries to remove the object that has the same value
        as the index of 'number'. If you're using IndexOf, then you also want
        to use RemoveAt rather than Remove.

        Which you probably already knew, but IMHO it's worth correcting the typo
        for others who might be reading.


        --
        David
        dfoster at
        hotpop dot com

        Comment

        • Peter Koen

          #5
          Re: ArrayList problem

          David <dfoster@127.0. 0.1> wrote in
          news:slrnbpobka .lg7.dfoster@wo ofix.local.dom:

          [color=blue]
          > Which you probably already knew, but IMHO it's worth correcting the typo
          > for others who might be reading.[/color]

          Yeah, sorry, was just a typo. Thanks for clearing this!



          --
          best regards

          Peter Koen
          -----------------------------------
          MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

          Comment

          • Jerry S

            #6
            Re: ArrayList problem

            Thanks Peter.

            I should, of course, have been using RemoveAt.

            Thanks for the TrimToSize tip!


            On Sun, 26 Oct 2003 11:31:25 -0800, Peter Koen
            <koen-newsreply&snusn u.at> wrote:

            [color=blue]
            >call the Methods on the Classname.
            >
            >it should be more like this:
            >
            >public class myClass
            >{
            > ArrayList al = new ArrayList();
            >
            > public void ALRemove(int number)
            > {
            > al.Remove(al.In dexOf(number));
            > }
            >}
            >
            >and you shouldn't always call TrimToSize. That function needs quite some
            >processing power. you should call it only when you removed many Items
            >from the list. a good practice would be to call it when more than 10% of
            >the items have been removed. with integers only in the list it would be
            >enough to call it when you removed at least 100 items.[/color]

            Comment

            • Jerry S

              #7
              Re: ArrayList problem

              Thanks David.

              RemoveAt sorted it out.

              I wasn't actually calling the object ArrayList - that was for the
              purposes of illustration!

              Jerry


              On Sun, 26 Oct 2003 12:19:39 -0800, David <dfoster@127.0. 0.1> wrote:
              [color=blue]
              >In article <6k6opvg8d98nqq ajlfk7984ok37uo gah0n@4ax.com>, Jerry S wrote:[color=green]
              >> Hi
              >>
              >> I've got an arraylist of integers.
              >>
              >> I want to be able to remove an integer from the list using a method
              >> like this:
              >>
              >> public void ArrayListRemove (int indexnum)
              >> {
              >> ArrayList.Remov e(ArrayList.Ind exOf(indexnum)) ;
              >> ArrayList.TrimT oSize();
              >> }[/color]
              >
              >Is indexnum the index of the number you want to remove, or the value of
              >the number you want to remove? If indexnum is the value you want to
              >remove, you probably want either..
              >
              > arrayList.Remov eAt(arrayList.I ndexOf(indexnum ))
              >or
              > arrayList.Remov e(indexnum)
              >
              >Obviously, the second one is simpler.
              >
              >Although if you've already determined the index number then
              >
              > arrayList.Remov eAt(indexnum)
              >
              >will do the job.
              >[color=green]
              >>
              >> It isn't working. I can't figure out why...
              >>
              >> I seem to be getting an Invalid Object reference error when it runs
              >> (that's what the debugger says, anyway!).[/color]
              >
              >Well, that seems to point to deeper problems, and suggests that the
              >ArrayList object has fallen out of scope someplace else in your code.
              >
              >BTW, are you really naming the object "ArrayList" ? It's incredibly
              >confusing to give a class instance the same name as the class itself,
              >IMHO.
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: ArrayList problem

                Jerry S <jerry@philosop hers.co.uk> wrote:[color=blue]
                > RemoveAt sorted it out.
                >
                > I wasn't actually calling the object ArrayList - that was for the
                > purposes of illustration![/color]

                There's a very important point here, which is that you should always
                post the *actual* code, rather than copying it out. It's very easy to
                end up going down lots of blind alleys because of errors put in by
                copying. Equally, it's possible (although rare) that the *real* error
                may be *removed* by a typo.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...