我想做一个整数值的数组列表,并运行一些基本的数学运算,如下所示。在c#unity中定义数组列表。
int dice1 = 4;
int dice2 = 3;
int dice3 = 6;
int dice4 = 4;
int dice 5 = 5;
ArrayList numbers = new ArrayList();
numbers[4] = dice5;
numbers[3] = dice4;
numbers[2] = dice3;
numbers[1] = dice2;
numbers[0] = dice1;
numbers[3] = numbers[3] * numbers[2];
但是,计算机不允许我这样做,会产生一个错误“操作‘*’不能适用于类型‘对象’和‘对象’的操作数”。我该如何解决?我认为我必须将数组列表定义为一个整数数组......但是我不太确定。请保持简单的答案,因为我对C#团队相当陌生。
谢谢!使用数组列表
使用List<int>
或int[]
然后所含的对象
非常感谢您的回答。非常详细,非常有帮助!我使用ArrayLists的原因是我可以从中删除元素。 –
在这种情况下,您也可以从List <>中删除东西。这里有一堆关于它的方法! https://msdn.microsoft.com/en-us/library/s6hkc2c4(v=vs.110).aspx –