Re: How to count value in a ArrayList
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Sat, 24 May 2008 16:20:53 -0700
On Sat, 24 May 2008 15:23:21 -0700, j1mb0jay <j1mb0jay@xxxxxxxxx> wrote:
From reading the OP, i thought that they where inserting double values
into a data structure and wanted to make sure that the value that they
were adding was not already stored in the data structure.
That's not how I read the original message at all. He specifically states "I _have_ an ArrayList" (emphasis mine) and "that _is_ sorted". Granted, maybe English isn't his first language and he's misusing the tenses. But I don't see any reason to _assume_ that. If he wants to clarify, that's fine. But the English as stated doesn't imply anything at all about building the data structure. It's there already, and he just wants to interact with it.
To me this
means they will need to compare every insert item with ever single item
in the list O(n). Doing this with a ArrayList is a lot slower than a hash
table, as you could hash the double value to find out which linked list
the value would be stored in, then just check the few values in the list.
Well, if your interpretation of the OP was correct, then the above would be relevant to the OP. But still not relevant to the statement about the cost of adding an element to the data structure.
When an array list is full is needs to be cloned to another larger array
list.
As does a hash table, albeit under somewhat different rules.
I thought that hash tables where are array of linked lists (Also
known as a bucket array) this means they can never be full (will use SWAP/
Virtual Memory when RAM is full), but the more items that are in each
linked list the slower the search time becomes, the insert time of a
linked list is always O(1) if you are adding to the tail of the list.
I haven't checked the .NET hash table implementation specifically. But presumably they use a relatively common technique of having a fixed-sized array of bins that grows as the hash table accumulates items. This allows a hash table with relatively few elements to not take up too much memory, but still allows for minimizing collisions for hash tables that have a lot of elements.
Some implementations use bins that are either arrays or linked lists, while others simply use the next available spot in the hash table (and undoubtedly there are other implementations I'm not thinking of at the moment). Either way, the lookup part of the hash table is only ever as big as needed for the number of elements in the table.
If the OP is using a ArrayList and would like to keep the items in order
when inserting a new item this would mean moving all of items along one
in the list, again another slow process, maybe a tree of some kind would
be a better option.
Yes. But the OP never said he wants to do that.
I was always taught to stay away from ArrayList at all costs. Arrays are
fine if you know the amount of items that you would like to store, and
they have a fast data access compared to a list as you do not have to
iterate all of the items to find the one you require.
Don't confuse ArrayList with an array _or_ a list. It is neither, even though it shares some features with each.
In any case, arrays, lists, or ArrayLists, none deserve being considered as "stay away from at all costs". They each have their purposes, and they are perfectly valid data structures for those purposes.
Pete
.
- References:
- How to count value in a ArrayList
- From: Tony
- Re: How to count value in a ArrayList
- From: Ignacio Machin ( .NET/ C# MVP )
- Re: How to count value in a ArrayList
- From: Arne Vajhøj
- Re: How to count value in a ArrayList
- From: Arne Vajhøj
- Re: How to count value in a ArrayList
- From: Peter Duniho
- How to count value in a ArrayList
- Prev by Date: Re: Encapsulation
- Next by Date: Re: Really basic C# and Visual Studio 2008 question
- Previous by thread: Re: How to count value in a ArrayList
- Next by thread: Re: How to count value in a ArrayList
- Index(es):
Relevant Pages
|