arrayList GetRange问​​题 [英] arrayList GetRange question

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

问题描述

大家好,问题是使用"GetRange"方法向玩家1的arrayList添加新卡是否正确?

hi everyone,The question is that is it right to use "GetRange" method to add new cards to player1 '' s arrayList?

ArrayList cards = new ArrayList();
// i also create cards arrayList with "for".

ArrayList player1 = new ArrayList();
Random rnd = new Random();

for (int i = 0; i < 5; i++)
{
  int card = rnd.Next(0, cards.Count);
  player1.Add(cards.GetRange(card, 1))
  cards.RemoveAt(card);
}

推荐答案

如果您仅获得一项,我认为GetRange并不是一个好用处.当您需要获得多个项目时,我会更多地使用它.

我只会用:

If you are only getting one item, I don''t think GetRange is a good use. I would use it more for when you need to get more than one item.

I would just use:

player1.Add(cards[card]);


语法上正确(因为Add方法接受objectArrayList,返回值GetRangeobject)但可能不是您想要的,请尝试:
It is syntactically correct (because a Add method accepts an object and an ArrayList, the return value of GetRange, is an object) but probably is NOT what you want to do, try instead:
player1.Add(cards[card]));



这种歧义性"是 strong 观点,赞成 strong类型的集合(:-)),即通用集合.



This ''ambiguity'' is a strong point in favour of strong typed collections ( :-) ), that is generic collections.


可以工作,但是肯定有更清晰的方法可以做到这一点.我为您提供的一项建议是将ArrayList更改为通用List< int>.然后只需索引列表即可检索您的值.

即类似以下内容

will work but there are certainly clearer ways to do this. One recommendation I have for you is to change from an ArrayList to a generic List<int> and then just index the List to retrieve your value.

i.e. something like the following

List<int> cards = new List<int>();
List<int> player1 = new List<int>();
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
    int card = rnd.Next(0, cards.Count);
    player1.Add(cards[card]);
    cards.RemoveAt(card);
}


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

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