在c#unity中定义数组列表。

在c#unity中定义数组列表。,c#,arrays,unity3d,arraylist,C#,Arrays,Unity3d,Arraylist,我试图创建一个整数值数组列表,并运行一些基本的数学运算,如下所示 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]

我试图创建一个整数值数组列表,并运行一些基本的数学运算,如下所示

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];
但是,计算机不允许我这样做,并产生错误“运算符”*“不能应用于'object'和'object'类型的操作数。”。我该如何解决这个问题?我想我必须将数组列表定义为一个整数数组。。。但是我不太确定。请保持答案简单,因为我对C#unity很陌生

谢谢

避免使用数组列表

使用
List
int[]


然后将包含的对象类型化,而不是对象

ArrayList将所有内容存储为“对象”,基本上是C#中最基本的类型。你有几个选择。如果要继续使用ArrayList,则需要对要乘以的内容进行强制转换,如:

numbers[3] = ((int)numbers[3]) * ((int)numbers[2])
或者,您可以放弃ArrayList,使用更现代的列表类型。您需要使用System.Collections.Generic将
添加到顶部,然后您的代码将如下所示:

int dice1 = 4;
int dice2 = 3;
int dice3 = 6;
int dice4 = 4;
int dice5 = 5;

List<int> numbers = new List<int>(); //List contains ints only
    numbers[4] = dice5;
    numbers[3] = dice4;
    numbers[2] = dice3;
    numbers[1] = dice2;
    numbers[0] = dice1;

numbers[3] = numbers[3] * numbers[2]; //Works as expected

您需要将对象解析为字符串,然后再解析为int值,然后将其与*运算符一起使用。但首先必须使用空值初始化arraylist,然后分配数值,以便, 使用下面的代码,我为您做了明确的更改

int dice1 = 4;
    int dice2 = 3;
    int dice3 = 6;
    int dice4 = 4;
    int dice5 = 5;
    int capacity=5;
    ArrayList numbers = new ArrayList(capacity);
    for (int i = 0; i < capacity;i++ )
    {
        numbers.Add(null);
    }
    numbers[4] = dice5;
    numbers[3] = dice4;
    numbers[2] = dice3;
    numbers[1] = dice2;
    numbers[0] = dice1;

    numbers[3] = (int.Parse(numbers[3].ToString()) * int.Parse(numbers[2].ToString()));
    print(numbers[3]);
int dice1=4;
int=2=3;
int=3=6;
int=4;
int=5;
int容量=5;
ArrayList编号=新的ArrayList(容量);
对于(int i=0;i
您可以在一行中完成:

List<int> numbers = new List<int>{ 4, 3, 6, 4, 5 };
列表编号=新列表{4,3,6,4,5};

非常感谢您的回答。非常详细,非常有用!我之所以使用ArrayList,是因为我可以从中删除元素。在这种情况下,您也可以从列表中删除内容。这里有很多方法!
List<int> numbers = new List<int>{ 4, 3, 6, 4, 5 };