ArrayList - GetRange [英] ArrayList - GetRange

查看:75
本文介绍了ArrayList - GetRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在我的应用程序中的某一点上,我有一个ArrayList对象,我需要在两个Arraylist对象中分解:开始部分到索引,结束部分来自某个指数。我现在正在使用ArrayList.GetRange,但文档声明此方法不会创建新的ArrayList对象,而是查看源ArrayList对象


ArrayList original

/ / ..

ArrayList l1 = original.GetRange(0,count)

ArrayList l2 = original.GetRange(count,untilEndOfList)

处理(l1)

处理(l2)

返回的ArrayList对象是否真的不是真正的独立ArrayList对象(即它们与原文)


如果是这样的话,我怎样才能制作两个与原版无关的真正单独的ArrayList对象。我希望每个ArrayList不会超过一行代码......我会非常失望


谢谢

汤姆。

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.

推荐答案

嗨汤姆


您可能想要使用ArrayList.Clone()代替摆脱不必要的元素。或者只是创建新的ArrayLists并使用与原始相同的引用填充它们。


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

{

if(i< count)

l1.Add(original [i]);

else

l2.Add(original [i]);

}


快乐的编码!

Morten Wennevik [C# MVP]
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]


另一种方式,如果你不需要保留原始列表。


ArrayList l1 = new ArrayList ();

while(original.Count> count)

{

l1.Add(original [count])

original.RemoveAt(count);

}


快乐的编码!

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 @ | / \ |蒂蒂____ AThotmail.com | / \ | @ PS0_N_>>写道:
TT ( Tom Tempelaere ) <=?Utf-8?B?VFQgKCBUb20gVGVtcGVsYWVyZSAp?= <_N_
0SP@|/\|titi____AThotmail.com|/\|@PS0_N_>> wrote:
在我的应用程序的某一点上,我有一个ArrayList对象,我需要在两个Arraylist对象中分解:开始部分到索引,以及某个索引的结尾部分。我现在正在使用
ArrayList.GetRange,但文档声明此方法不会创建新的ArrayList对象,而是查看源ArrayList
对象。

ArrayList original ;
// ...
ArrayList l1 = original.GetRange(0,count);
ArrayList l2 = original.GetRange(count,untilEndOfList);
处理(l1) ;
处理(l2);

返回的ArrayList对象是否真的不是真正的独立ArrayList对象(即它们与
相关联)原文)?

如果是这样的话,我怎样才能制作两个真正独立的ArrayList对象,而这些对象与原版无关。我希望这不会比每个ArrayList的代码行占用更多
......我真的很失望。
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.




最简单的方法(IMO)可以使用克隆或使用

ArrayList(ICollection):


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

ArrayList l2 = new ArrayList(original.GetRange(count,untilEndOfList));


-

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

如果回复小组,请不要给我发邮件



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


这篇关于ArrayList - GetRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆