Skip to main content
gamedev.net
🔒 Locked

C# ArrayList Add

Started by Yratelev May 2, 2004 at 11:11 AM 7 replies 700+ views
Original Post
Yratelev
Yratelev
Hi, when using arraylist.add() it always adds the object to the end of the array, and not the first empty slot. This means with a lot of adding and removing, the array list rapidly becomes huge, with most of it being empty. How to I avoid this, or does the arraylist add the object ito the first empty slot? thanks Yratelev
Yratelev
Zipster
Zipster
ArrayList.TrimToSize will shrink the capacity of your ArrayList to the actual number of elements.
Manip
Manip
myArrayList.TrimToSize();

SnprBoB86
SnprBoB86
TrimToSize will truncate the CAPACITY not compress the array to fill "empty" spaces. I do not know what is meant by empty.

When you remove an item from an ArrayList it compresses the array for you. I recomment you google for Anakrino, which is a .net decompiler and look at the ArrayList class in mscorlib.dll. You will see it is really not a class to be used for heavy insertions and deletions.

If you need random access, use System.Collections.Hashtable. Otherwise, roll your own linked list (it is extreamly easy to do in C#, shouldn''t take you more than 15 minutes)

-SniperBoB-
Zipster
Zipster
quote:
Original post by SnprBoB86
TrimToSize will truncate the CAPACITY not compress the array to fill "empty" spaces. I do not know what is meant by empty.

I simply think he had the wrong idea on how it was implemented. And BTW, TrimToSize does truncate the capacity, thanks for that

[edited by - Zipster on May 2, 2004 2:27:44 PM]
levendis
levendis
quote:
Original post by SnprBoB86
TrimToSize will truncate the CAPACITY not compress the array to fill "empty" spaces. I do not know what is meant by empty.

When you remove an item from an ArrayList it compresses the array for you. I recomment you google for Anakrino, which is a .net decompiler and look at the ArrayList class in mscorlib.dll. You will see it is really not a class to be used for heavy insertions and deletions.

If you need random access, use System.Collections.Hashtable. Otherwise, roll your own linked list (it is extreamly easy to do in C#, shouldn''t take you more than 15 minutes)

-SniperBoB-


I''d recommend googling for Rotor, it''s shared source released by Microsoft and includes the Base Class Libraries implementations. It''s a good reference.

Unfortunately System.Collections is lacking in several basic implementations (Linked-lists, sets, and trees), but there have been implementations of LinkedLists done in C# by third parties. It''s generally better to use something already debugged than reinventing your own. It''s just nicer if there''s an official version to reference
------http://www.livejournal.com/users/rain_is_wet/
ggs
ggs
quote:
Original post by levendis
Unfortunately System.Collections is lacking in several basic implementations (Linked-lists, sets, and trees), but there have been implementations of LinkedLists done in C# by third parties. It''s generally better to use something already debugged than reinventing your own. It''s just nicer if there''s an official version to reference

Link lists are downright trivial to implement in C#. But the lack of sets & trees is anoying.

Or a hashtable which uses bucketing instead of linear probing...
SnprBoB86
SnprBoB86
I think the problem with linked list and trees is that often you want your object to be a node in the tree or list, not referenced elsewhere.

See System.Windows.Forms.Control or System.Web.Control (double check me on the ASP.NET controls, I haven''t done much with them).

A lack of multiple inheritance makes this a difficult problem to solve.

-SniperBoB-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.