ArrayList problem

  • Thread starter Thread starter Jerry S
  • Start date Start date
J

Jerry S

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.Remove(ArrayList.IndexOf(indexnum));
ArrayList.TrimToSize();
}

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
 
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.Remove(ArrayList.IndexOf(indexnum));
ArrayList.TrimToSize();
}

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

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.IndexOf(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.
 
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.Remove(ArrayList.IndexOf(indexnum));
ArrayList.TrimToSize();
}

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.RemoveAt(arrayList.IndexOf(indexnum))
or
arrayList.Remove(indexnum)

Obviously, the second one is simpler.

Although if you've already determined the index number then

arrayList.RemoveAt(indexnum)

will do the job.
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!).

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.
 
ArrayList al = new ArrayList();

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

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.
 
Which you probably already knew, but IMHO it's worth correcting the typo
for others who might be reading.

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

I should, of course, have been using RemoveAt.

Thanks for the TrimToSize tip!
 
Thanks David.

RemoveAt sorted it out.

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

Jerry
 
Jerry S said:
RemoveAt sorted it out.

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

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.
 
Back
Top