Get the last item in ArrayList

C

Curious

I have an ArrayList outputList. I only need to get the last item:

int last = outputList.LastIndexOf(null);
ExtremeBucket lastItem =
(ExtremeBucket)outputList[last];

Please confirm if this is the right way. Thanks!
 
F

Family Tree Mike

No, to get the last item in the list, use

int last = outputList [outputList.Count () - 1];

I have no idea how you are converting an int to an ExtremeBucket though.
 
C

Curious

Family Tree Mike:

Thanks! FYI, I used the following approach you've suggested:

ExtremeBucket lastItem =
(ExtremeBucket)outputList[outputList.Count - 1];

Each item is type of ExtremeBucket.
 
J

Jack Jackson

Family Tree Mike:

Thanks! FYI, I used the following approach you've suggested:

ExtremeBucket lastItem =
(ExtremeBucket)outputList[outputList.Count - 1];

Each item is type of ExtremeBucket.

You should consider using the generic List instead of ArrayList. That
way your access to the list will be type safe.
 
F

Family Tree Mike

I agree with Jack, that List<ExtremeBucket> will be better for you in the
long run.
 
C

Curious

I agree with Jack, that List<ExtremeBucket> will be better for you in the
long run.

I do intend to use List<ExtremeBucket>. However, since this must be
coded in Visual Studio 2003 (which is compatible with .NET 1.1), I
have to use ArrayList because it doesn't recognize generic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top