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. 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]
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]
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
----- 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.
----- 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.
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.
"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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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:
...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |