PC Review


Reply
Thread Tools Rate Thread

ArrayList vs. List

 
 
puzzlecracker
Guest
Posts: n/a
 
      16th Oct 2008
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
 
Reply With Quote
 
 
 
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      16th Oct 2008
On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.com> wrote:
> 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
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      16th Oct 2008
On Oct 16, 11:27*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.com> wrote:
> On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.com> wrote:
>
> > 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


I think it is part of 3.0+, I need to support .NET 2.0... suggestions?
 
Reply With Quote
 
Mythran
Guest
Posts: n/a
 
      16th Oct 2008


"puzzlecracker" <(E-Mail Removed)> wrote in message
news:3815ca86-54a8-4039-b6b1-(E-Mail Removed)...
> On Oct 16, 11:27 am, "Ignacio Machin ( .NET/ C# MVP )"
> <ignacio.mac...@gmail.com> wrote:
>> On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.com> wrote:
>>
>> > 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

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

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Oct 2008
On Oct 16, 5:04*pm, puzzlecracker <ironsel2...@gmail.com> wrote:
> > 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?


What makes you think that? From MSDN:
http://msdn.microsoft.com/en-us/library/ms132474.aspx

<quote>
Version Information
..NET Framework
Supported in: 3.5, 3.0, 2.0
</quote>

Jon
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      16th Oct 2008
On Oct 16, 12:41*pm, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> On Oct 16, 5:04*pm, puzzlecracker <ironsel2...@gmail.com> wrote:
>
> > > 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?

>
> What makes you think that? From MSDN:http://msdn.microsoft.com/en-us/library/ms132474.aspx
>
> <quote>
> Version Information
> .NET Framework
> Supported in: 3.5, 3.0, 2.0
> </quote>
>
> Jon


I was wrong, it's supported... I first encountered it in the book
on .Net 3.0, hence the erroneous assumption.
 
Reply With Quote
 
Rudy Velthuis
Guest
Posts: n/a
 
      16th Oct 2008
Mythran wrote:

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

--
Rudy Velthuis http://rvelthuis.de

"We totally deny the allegations, and we are trying to identify
the allegators."
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      16th Oct 2008
On Oct 16, 2:22*pm, "Rudy Velthuis" <newsgro...@rvelthuis.de> wrote:
> Mythran wrote:
> > 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.
>
> --
> 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?
 
Reply With Quote
 
Rudy Velthuis
Guest
Posts: n/a
 
      16th Oct 2008
puzzlecracker wrote:

> 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
 
Reply With Quote
 
Rudy Velthuis
Guest
Posts: n/a
 
      16th Oct 2008
Peter Duniho wrote:

> On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker
> <(E-Mail Removed)> wrote:
>
> > Does arraylist uses a traditional array and List uses nodes to
> > store elements, much like in Java?

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

--
Rudy Velthuis http://rvelthuis.de

"No Sane man will dance." -- Cicero (106-43 B.C.)
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about List<T> and ArrayList Tony Johansson Microsoft C# .NET 0 9th Nov 2010 10:52 AM
migrating to List<T> from ArrayList David C Microsoft C# .NET 6 20th May 2008 11:09 PM
Populate Combo/List Box with ArrayList Greg Microsoft VB .NET 4 19th Apr 2008 08:33 PM
ArrayList or List<> csharpula csharp Microsoft C# .NET 2 27th Nov 2007 05:36 PM
ArrayList vs. List<> Zytan Microsoft C# .NET 44 17th May 2007 12:43 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:56 AM.