ArrayList vs. List

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

I noticed in my code, which is essentially an api for the client, that
I return ArrayList object to the client. In my case, list consist of
some objects that user only need to iterate over, hence no
modification to list is done after it filled by me.

What do you think is the best data structure for this requirement?

Thanks
 
I noticed in my code, which is essentially an api for the client, that
I return ArrayList object to the client. In my case, list consist of
some objects that user only need to iterate over, hence no
modification to list is done after it filled by me.

What do you think is the best data structure for this requirement?

Thanks

You should return a readonly collection. Take a look at
ReadOnlyCollection , it's in System.Collections.ObjectModel
 
You should return a readonly collection. Take a look at
ReadOnlyCollection , it's in System.Collections.ObjectModel

I think it is part of 3.0+, I need to support .NET 2.0... suggestions?
 
puzzlecracker said:
I think it is part of 3.0+, I need to support .NET 2.0... suggestions?

Why not use an ArrayList internally and then convert the ArrayList to an
array and return that?

Kip
 
Mythran said:
Why not use an ArrayList internally and then convert the ArrayList to
an array and return that?

Perhaps because "converting the ArrayList to an array" would mean
copying data from the ArrayList to the array, perhaps even multiple
times, and that is relatively slow, if compared to giving people direct
(but readonly) access.
 
Perhaps because "converting the ArrayList to an array" would mean
copying data from the ArrayList to the array, perhaps even multiple
times, and that is relatively slow, if compared to giving people direct
(but readonly) access.

--
Rudy Velthuis        http://rvelthuis.de

"We totally deny the allegations, and we are trying to identify
 the allegators."

Does arraylist uses a traditional array and List uses nodes to store
elements, much like in Java?
 
puzzlecracker said:
Does arraylist uses a traditional array and List uses nodes to store
elements, much like in Java?

No, List<T> is simply the generic equivalent of ArrayList. Both
internally use an array to hold the list elements. But ArrayList uses
an array of objects, while List<T> uses an array of instances of type T.

--
Rudy Velthuis http://rvelthuis.de

"Barabási's Law of Programming: Program development ends when the
program does what you expect it to do — whether it is correct or
not." -- Albert-László Barabási
 
Peter said:
ArrayList, List<T>, and Java's ArrayList all use arrays to store
elements.

Quite right, but I guess he meant that Java's List class is in fact
implemented as a doubly linked list.
 
Peter said:
Java doesn't have a List class. It does have a doubly-linked list
class called LinkedList,

Hmmm... then I must be confusing it with one of the many other
frameworks I have seen lately. <g>

Sorry.
 
Rudy said:
Hmmm... then I must be confusing it with one of the many other
frameworks I have seen lately. <g>

C++ std::list<typename T> is a linked list like C# LinkedList<T>.
 
Ben said:
C++ std::list<typename T> is a linked list like C# LinkedList<T>.

Ah, indeed. I guess I was indeed thinking of std::list<T>.
--
Rudy Velthuis http://rvelthuis.de

"Imagine if every Thursday your shoes exploded if you tied them
the usual way. This happens to us all the time with computers,
and nobody thinks of complaining." -- Jeff Raskin
 
Back
Top