ArrayList - GetRange

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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);
else
l2.Add(original);
}

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_
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));
 
----- 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)
els
l2.Add(original)

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 l1 = original.GetRange( 0, count )
ArrayList l2 = original.GetRange( count, tillEndOfList )
Processing( l1 )
Processing( l2 )
independent ArrayList objects (ie that they are tied to th
original)
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 - <[email protected]
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) said:
----- 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);
else
l2.Add(original);
}
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.
 
Bonj of Beeeeeves said:
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.
 
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|[email protected]|/\|APS0_N_> said:
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 said:
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/


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.

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top