1 Replies - 9580 Views - Last Post: 04 April 2008 - 09:19 AM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Create a List<T> at a default size or empty at initialization

Posted 04 April 2008 - 08:50 AM

As usual, I just have a question on what people other than myself would do:

I have an array list that is typically populated with anywhere from 100 to 250 string objects. Would you guys typically initialize the arrayList to a size of 250, forgo a minuscule amount of memory, and try and save time when adding elements to the List?

I think that resizing the List is expensive...
Is This A Good Question/Topic? 0
  • +

Replies To: Create a List<T> at a default size or empty at initialization

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Create a List<T> at a default size or empty at initialization

Posted 04 April 2008 - 09:19 AM

Nope, never give the things an inital capacity; they should bloody well be smart enough to get along without that. The only real cost would be speed over memory; I'll take the minor speed hit.

View Postkillnine, on 4 Apr, 2008 - 11:50 AM, said:

I think that resizing the List is expensive...


Depends how the underlying list is implemented. A reasonable implemention would be some kind of linked list, in which case the expense is just creating and adding nodes, plus extra house keeping, like maybe an object hash index. Regardless of size, you're always going to be creating and adding nodes. If this is the case, then specifing an initial size could be very trivial indeed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1