ArrayList vs. List

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

Ignacio Machin ( .NET/ C# MVP )

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
 
P

puzzlecracker

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?
 
M

Mythran

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
 
R

Rudy Velthuis

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.
 
P

puzzlecracker

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?
 
R

Rudy Velthuis

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
 
R

Rudy Velthuis

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.
 
R

Rudy Velthuis

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.
 
B

Ben Voigt [C++ MVP]

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>.
 
R

Rudy Velthuis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top