473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Alternative to ArrayList.Add()

Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(MyShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add()
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.
Nov 15 '05 #1
2 6186
SortedList would be slower since it is a specialization of ArrayList in a way.
A Hashtable would be faster to insert, but possibly slower to use later.

I don't see where pre-initing a 200 element ArrayList and then using that would
be very slow though. Once the ArrayList gets a good number of items, it grows
itself pretty darn fast.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Tristan" <tr************@yahoo.co.uk> wrote in message
news:7f**************************@posting.google.c om...
Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(MyShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add()
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.

Nov 15 '05 #2
Define "Shape"?;

It might not be the collection that's the problem, but the amount and the
size of objects that you're shoving into it...

Also, it would be a good idea to write some code that tries to guess what
the initial capacity capacity of the ArrayList should be...

Remember:
"Capacity is always greater than or equal to Count. If Count exceeds
Capacity while adding elements, the capacity of the list is doubled by
automatically reallocating the internal array."

Reallocation of Large arrays can be slow - if you have an idea of how many
elements you're gonna have then add 10%, you'll probably see a significant
gain in perf.

And don't forget to TrimToSize().

Hope it helps;
Josh.

"Tristan" <tr************@yahoo.co.uk> wrote in message
news:7f**************************@posting.google.c om...
Hi,

My code reads through a file of shapes adding each
one to an ArrayList as it does so.

The problem is that for files with large numbers of
shapes, calling ArrayList.Add(MyShape) is
exceptionally slow.

There is no way of knowing how many shapes need to
be read in so I can't set the ArrayList capacity
and use array indexing to add the shape objects.

I am prepared to use another collection if it
will be quicker and allow quick addition and
retrieval of objects (not so worried about
insertions and deletions), would SortedList.Add()
be any quicker?

Is there any way around this problem?

Any help will be much appreciated.

Tristan.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
3
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
4
by: tma | last post by:
I'm trying to save code to a session object like the following: dim oAppList as arraylist dim oApp as someclass Code to manipulate oApp... .... .... oApplist.Add(oApp)...
6
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow =...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
9
by: Matt Bailey | last post by:
I am writing an ASP page with a C# backend. This page uses a StreamReader to input a file and pull out a specific section of the text based on markers inside. While the code I have works, I get a...
1
by: Greg Larsen | last post by:
How to I populate and reference multi-dimensional ArrayList. I would like to populate an Array list with the Add method something like so: ArrayList a = new ArrayList; ArrayList b = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.