473,513 Members | 4,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList vs. List

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
Oct 16 '08 #1
14 2169
On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.comwrote:
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
Oct 16 '08 #2
On Oct 16, 11:27*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.comwrote:
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?
Oct 16 '08 #3


"puzzlecracker" <ir*********@gmail.comwrote in message
news:38**********************************@k16g2000 hsf.googlegroups.com...
On Oct 16, 11:27 am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
>On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gmail.comwrote:
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

Oct 16 '08 #4
On Oct 16, 5:04*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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
Oct 16 '08 #5
On Oct 16, 12:41*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Oct 16, 5:04*pm, puzzlecracker <ironsel2...@gmail.comwrote:
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.
Oct 16 '08 #6
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."
Oct 16 '08 #7
On Oct 16, 2:22*pm, "Rudy Velthuis" <newsgro...@rvelthuis.dewrote:
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?
Oct 16 '08 #8
On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker <ir*********@gmail.com>
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.
Oct 16 '08 #9
puzzlecracker wrote:
Does arraylist uses a traditional array and List uses nodes to store
elements, much like in Java?
No, List<Tis 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<Tuses 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
Oct 16 '08 #10
Peter Duniho wrote:
On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker
<ir*********@gmail.com 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.)
Oct 16 '08 #11
On Thu, 16 Oct 2008 12:01:30 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
Peter Duniho wrote:
>On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker
<ir*********@gmail.com 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.
Java doesn't have a List class. It does have a doubly-linked list class
called LinkedList, and like the ArrayList class it does implement the List
interface. But "puzzlecracker" didn't mention any LinkedList class at
all, in Java or C#.

In any case, it's my hope that my reply answered his question, in spite of
the inaccuracies in that question.

Pete
Oct 16 '08 #12
Peter Duniho wrote:
Quite right, but I guess he meant that Java's List class is in fact
implemented as a doubly linked list.

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 Velthuis http://rvelthuis.de

"A low voter turnout is an indication of fewer people going to
the polls." -- George W. Bush
Oct 16 '08 #13
Rudy Velthuis wrote:
Peter Duniho wrote:
>>Quite right, but I guess he meant that Java's List class is in fact
implemented as a doubly linked list.

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>
C++ std::list<typename Tis a linked list like C# LinkedList<T>.
C++ std::vector<typename Tis a wrapped array like ArrayList or List<T>.
>
Sorry.

Oct 16 '08 #14
Ben Voigt [C++ MVP] wrote:
Rudy Velthuis wrote:
Peter Duniho wrote:
Quite right, but I guess he meant that Java's List class is in
fact implemented as a doubly linked list.
>
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>

C++ std::list<typename Tis 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
Oct 16 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4603
by: Jamus Sprinson | last post by:
Before I continue, I'm going to begin by saying I'm not by any means an expert- I've been using .NET with C# for about 4 months now, and basically just learning by example and docs. A game project I work on uses Xml serialization to store game objects for loading. One of the techniques we use is to make an array property and use the set...
7
14248
by: William Stacey [MVP] | last post by:
I can think of a couple ways to do this, but was wonder what the fastest way you can think of. Want to take any arraylist of > 1 element and make the first element the last, and all the rest of the elements will move up one position. This will be called a lot, so looking for speed. This just saving the index 0 as tmp and then enum the list...
6
12143
by: Michael C | last post by:
Is it possible to use an ArrayList inside a struct? I keep running into a null reference exception when I try to Add to the ArrayList in the struct, and it won't let me initialize the ArrayList in the struct. Any ideas? Thanks, Michael C.
8
11007
by: Sek | last post by:
Folks, I have an ArrayList of integers. I have sorted the list already. Now, i want to find the index of the first element that is greater than a given number. How to accomplish this in C#?
20
5948
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim...
18
4705
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class...
1
8363
by: KK | last post by:
Dear All I have a class whose methods are getting called from multiple threads in my application. For example class DataDistribution { private ArrayList datset; public DataDistribution() { this.datset = new ArrayList();
6
5788
by: Pavel Maly | last post by:
Hi, how do I access values in an ArrayList which is a part of another ArrayList? I know I can define a class and then it is quite simple, but this is just an auxiliary application to compute some values used later in the program. There must be a simpler way to achieve this without defining any (even temporary) class. Simple example: ...
3
3782
by: =?Utf-8?B?R3JlZw==?= | last post by:
I''ve been working with a CSharp web-site (and am new to it). I have a javascript function that accepts variables from my CSharp code. It all works fine, with the exception of one variable type. I created an ArrayList variable in my CSharp program and it collects the information i need with no problem. I pass it to my javascript with no...
6
3415
by: Slickuser | last post by:
Hi, I am picking up C#.net and I'm trying to add many values to one single key at different time in a loop. If the key already exist, append new value to previous? I'm not sure how to do that with ArrayList, any help? Thanks. Hashtable data = new Hashtable(); if (!data.Contains(key)) {
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.