473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.GetRange 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.GetRange( 0, count )
ArrayList l2 = original.GetRange( 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 4295
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.RemoveAt(count);
}

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
TT ( Tom Tempelaere ) <=?Utf-8?B?VFQgKCBUb20gVGVtcGVsYWVyZSAp?= <_N_
0SP@|/\|titi____AThotmail.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.GetRange 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.GetRange( 0, count );
ArrayList l2 = original.GetRange( 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(ICollection):

ArrayList l1 = new ArrayList(original.GetRange(0, count));
ArrayList l2 = new ArrayList(original.GetRange(count, tillEndOfList));

--
Jon Skeet - <sk***@pobox.com>
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?VFQgKCBUb20gVGVtcGVsYWVyZSAp?= <_N
0SP@|/\|titi____AThotmail.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.GetRange 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.GetRange( 0, count )
ArrayList l2 = original.GetRange( 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(ICollection)

ArrayList l1 = new ArrayList(original.GetRange(0, count))
ArrayList l2 = new ArrayList(original.GetRange(count, tillEndOfList))

--
Jon Skeet - <sk***@pobox.com
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____AThotmail.com|/\|@PS0_N_> wrote in
message news:A7**********************************@microsof t.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" <1234512345789123456789> wrote in message
news:uY**************@TK2MSFTNGP09.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(myVector.begin()+from, 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*******@hotmail.com|/\|APS0_N_>> wrote:
"Bonj of Beeeeeves" <1234512345789123456789> wrote in message
news:uY**************@TK2MSFTNGP09.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.GetRange (from, to));

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

--
Jon Skeet - <sk***@pobox.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotmail.com|/\|APS0_N_>> wrote:
"Bonj of Beeeeeves" <1234512345789123456789> wrote in message
news:uY**************@TK2MSFTNGP09.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.GetRange (from, to));

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

--
Jon Skeet - <sk***@pobox.com>
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
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...
3
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...
6
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 =...
4
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...
18
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...
11
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...
4
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)...
6
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...
3
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: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.