473,802 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList - GetRange

Hi

At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I am now using ArrayList.GetRa nge but the documentation states that this method does not create new ArrayList objects but views into the source ArrayList object

ArrayList original
// ..
ArrayList l1 = original.GetRan ge( 0, count )
ArrayList l2 = original.GetRan ge( count, tillEndOfList )
Processing( l1 )
Processing( l2 )

Is it true that the ArrayList objects returned do not act as true independent ArrayList objects (ie that they are tied to the original)

If so then how can I make two true seperate ArrayList objects that are not tied to the original. I hope that this doesn't take up more than one line of code per ArrayList ... I'd be really disappointed

Thank you
Tom.
Nov 16 '05 #1
9 4315
Hi Tom

You may want to use ArrayList.Clone () instead and get rid of the unwanted elements. Or just create new ArrayLists and fill them with the same references as in the original.

for(int i = 0; i < original.Count; i++)
{
if(i < count)
l1.Add(original[i]);
else
l2.Add(original[i]);
}

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Another way, if you don't need to preserve the original list.

ArrayList l1 = new ArrayList();
while(original. Count > count)
{
l1.Add(original[count])
original.Remove At(count);
}

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
TT ( Tom Tempelaere ) <=?Utf-8?B?VFQgKCBUb20 gVGVtcGVsYWVyZS Ap?= <_N_
0SP@|/\|titi____AThot mail.com|/\|@PS0_N_>> wrote:
At one point in my application I have a single ArrayList object that
I need to break up in two Arraylist objects: the beginning part up to
an index, and the ending part from a certain index. I am now using
ArrayList.GetRa nge but the documentation states that this method does
not create new ArrayList objects but views into the source ArrayList
object.

ArrayList original;
// ...
ArrayList l1 = original.GetRan ge( 0, count );
ArrayList l2 = original.GetRan ge( count, tillEndOfList );
Processing( l1 );
Processing( l2 );

Is it true that the ArrayList objects returned do not act as true
independent ArrayList objects (ie that they are tied to the
original)?

If so then how can I make two true seperate ArrayList objects that
are not tied to the original. I hope that this doesn't take up more
than one line of code per ArrayList ... I'd be really disappointed.


The easiest way (IMO) to do this would either be with Clone or using
ArrayList(IColl ection):

ArrayList l1 = new ArrayList(origi nal.GetRange(0, count));
ArrayList l2 = new ArrayList(origi nal.GetRange(co unt, tillEndOfList)) ;

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
----- Morten Wennevik wrote: ----
Hi To

You may want to use ArrayList.Clone () instead and get rid of the unwanted elements. Or just create new ArrayLists and fill them with the same references as in the original

for(int i = 0; i < original.Count; i++

if(i < count
l1.Add(original[i])
els
l2.Add(original[i])

Happy coding
Morten Wennevik [C# MVP

Hi Morten

Well Morten, I am quite disappointed, coming from C++ where the standard library offers truly wonderful generic collections & generic algorithms (using templates) that can do similar things in suprisingly little code. Even for simple operations like this, .NET forces me to write more code than is necessary

I love C# and .NET, but I get the feeling that the collection classes can be improved _a lot_. Hopefully with generics .NET will seperate algorithms from container functionality and thus make life of developers a little easier (like in C++). We'll see

Enfin, I'll just have to code it myself I guess

Thank you
Tom.
Nov 16 '05 #5
----- Jon Skeet [C# MVP] wrote: ----
TT ( Tom Tempelaere ) <=?Utf-8?B?VFQgKCBUb20 gVGVtcGVsYWVyZS Ap?= <_N
0SP@|/\|titi____AThot mail.com|/\|@PS0_N_>> wrote
At one point in my application I have a single ArrayList object tha
I need to break up in two Arraylist objects: the beginning part up t
an index, and the ending part from a certain index. I am now usin
ArrayList.GetRa nge but the documentation states that this method doe
not create new ArrayList objects but views into the source ArrayLis
object
ArrayList original

// ..
ArrayList l1 = original.GetRan ge( 0, count )
ArrayList l2 = original.GetRan ge( count, tillEndOfList )
Processing( l1 )
Processing( l2 )
Is it true that the ArrayList objects returned do not act as tru

independent ArrayList objects (ie that they are tied to th
original)
If so then how can I make two true seperate ArrayList objects tha

are not tied to the original. I hope that this doesn't take up mor
than one line of code per ArrayList ... I'd be really disappointed


The easiest way (IMO) to do this would either be with Clone or using
ArrayList(IColl ection)

ArrayList l1 = new ArrayList(origi nal.GetRange(0, count))
ArrayList l2 = new ArrayList(origi nal.GetRange(co unt, tillEndOfList))

--
Jon Skeet - <sk***@pobox.co m
http://www.pobox.com/~skee

Hi there Jon

So this constructor acts like a copy constructor (speaking in C++ terms)? Nice, thank you very much. I was starting to worry that I would have to code a seperate function just to do that.

Thanks again Jon
Tom.
Nov 16 '05 #6
Wrong. Jon's solution does exactly what you want
in as little as physically possible lines of code - definitely
less than C++ could do it in.
There's no problem with allocating new array lists because
the gc's going to be constantly runninig round after you
and if you localize as much as possible it'll have an easier job.

"TT (Tom Tempelaere)" <_N_0SP@|/\|titi____AThot mail.com|/\|@PS0_N_> wrote in
message news:A7******** *************** ***********@mic rosoft.com...
----- Morten Wennevik wrote: -----
Hi Tom

You may want to use ArrayList.Clone () instead and get rid of the unwanted elements. Or just create new ArrayLists and fill them with the same
references as in the original.
for(int i = 0; i < original.Count; i++)
{
if(i < count)
l1.Add(original[i]);
else
l2.Add(original[i]);
}
Happy coding!
Morten Wennevik [C# MVP]

Hi Morten,

Well Morten, I am quite disappointed, coming from C++ where the standard library offers truly wonderful generic collections & generic algorithms
(using templates) that can do similar things in suprisingly little code.
Even for simple operations like this, .NET forces me to write more code than
is necessary.
I love C# and .NET, but I get the feeling that the collection classes can be improved _a lot_. Hopefully with generics .NET will seperate algorithms
from container functionality and thus make life of developers a little
easier (like in C++). We'll see.
Enfin, I'll just have to code it myself I guess.

Thank you,
Tom.

Nov 16 '05 #7
"Bonj of Beeeeeves" <12345123457891 23456789> wrote in message
news:uY******** ******@TK2MSFTN GP09.phx.gbl...
Wrong. Jon's solution does exactly what you want
in as little as physically possible lines of code - definitely
less than C++ could do it in.
There's no problem with allocating new array lists because
the gc's going to be constantly runninig round after you
and if you localize as much as possible it'll have an easier job.


Could you please quote where I said or done something wrong?
<sigh>Top-posters ...</sigh>

In C++, if I'd write (T is any type that is suitable for std-containers like
std::vector, pick one):

std::vector<T> myVector;
/* ... fill myVector ... */

// initialize mySubRange with range [from, to) in myVector
std::vector<T> mySubRange(myVe ctor.begin()+fr om, myVector.begin( )+to);

Don't get fooled by the method calls 'begin' and 'end' , templates depend
heavily on inlining so the method calls will be inlined (these methods just
return a member variable).

So that is one constructor call, and one line of code. So, please tell where
I went wrong.
---
Tom Tempelaere
Nov 16 '05 #8
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotm ail.com|/\|APS0_N_>> wrote:
"Bonj of Beeeeeves" <12345123457891 23456789> wrote in message
news:uY******** ******@TK2MSFTN GP09.phx.gbl...
Wrong. Jon's solution does exactly what you want
in as little as physically possible lines of code - definitely
less than C++ could do it in.
There's no problem with allocating new array lists because
the gc's going to be constantly runninig round after you
and if you localize as much as possible it'll have an easier job.


Could you please quote where I said or done something wrong?


I suspect it was this:

<quote>
Even for simple operations like this, .NET forces me to write more code
than is necessary.
</quote>

ArrayList mySubRange = new ArrayList (myVector.GetRa nge (from, to));

is pretty short - one constructor call, one method call - in one line
of code.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotm ail.com|/\|APS0_N_>> wrote:
"Bonj of Beeeeeves" <12345123457891 23456789> wrote in message
news:uY******** ******@TK2MSFTN GP09.phx.gbl...
Wrong. Jon's solution does exactly what you want
in as little as physically possible lines of code - definitely
less than C++ could do it in.
There's no problem with allocating new array lists because
the gc's going to be constantly runninig round after you
and if you localize as much as possible it'll have an easier job.


Could you please quote where I said or done something wrong?


I suspect it was this:

<quote>
Even for simple operations like this, .NET forces me to write more code
than is necessary.
</quote>

ArrayList mySubRange = new ArrayList (myVector.GetRa nge (from, to));

is pretty short - one constructor call, one method call - in one line
of code.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet


I know Jon :) it was meant as a retorical question. I hate it when I say
several different things and then someone claims that I'm wrong. Hard to
tell what he thinks is wrong then. I am not wrong about the C++ part of what
I said, and he claimed I was so I had to reply of course. The 'definitely'
triggered my defense ;-).

Cheers,
---
Tom Tempelaere
Nov 16 '05 #10

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

Similar topics

7
455
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a arrayList to the datagrid. private void BindQuestions()
3
2863
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
6
5389
by: gane kol | last post by:
Hi, I have a code that creates a datatable from an arraylist, but i am getting an error in casting in for (int intRow = 0; intRow < alLCPlist.Count; intRow++) { DataRow drow = dtLCPack.NewRow(); int intColCount = dtLCPack.Columns.Count; ArrayList arrlRow = (ArrayList)alLCPlist; <== Specified cast
4
2829
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
18
4752
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 instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
11
16260
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first occurance index. Can anyone advise on this? Thanks, Simon
4
3613
by: MikeY | last post by:
Hi Everyone, I'm am trying to find the best method option for searching through an ArrayList and getting various items at a time. It could be 0 to 14, 15 to 25 etc,etc. I wish .GetRange(0,45) would give more options than just zero base to whatever. Right now, (unless there is a better option) I will keep calling the ArrayList at various times getting the 15 or less items at a time and put the items into another ArrayList for my usage....
6
3144
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
3
2632
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: code:------------------------------------------------------------------------------ class Program { static public ArrayList MyArrayList = new ArrayList();
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10304
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2966
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.