简体   繁体   English

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

[英]Defining an array list in c# unity.

I am trying to make an array list of integer values and run some basic math operations as seen below.我正在尝试创建一个整数值数组列表并运行一些基本的数学运算,如下所示。

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];

However, the computer does not allow me to do this and produces an error "Operator "*" cannot be applied to operands of the type 'object' and 'object'".但是,计算机不允许我这样做并产生错误“运算符“*”不能应用于“对象”和“对象”类型的操作数。 How do I fix this?我该如何解决这个问题? I think that I have to define the array list as an array of integers... however I am not too sure.我认为我必须将数组列表定义为整数数组......但是我不太确定。 Please keep the answers simple as I am quite new to C# unity.请保持答案简单,因为我对 C# unity 很陌生。

Thanks!谢谢!

ArrayList stores everything as an 'object', basically the most basic type something can be in C#. ArrayList 将所有内容存储为“对象”,基本上是 C# 中最基本的类型。 You have a few options.你有几个选择。 If you want to keep using ArrayList, then you'll need to do cast the things you're multiplying, like:如果你想继续使用 ArrayList,那么你需要做你正在乘的东西,比如:

numbers[3] = ((int)numbers[3]) * ((int)numbers[2])

Alternatively, you can ditch ArrayList and use the more modern List<> type.或者,您可以放弃 ArrayList 并使用更现代的 List<> 类型。 You need to add using System.Collections.Generic to the top, then your code will be like:您需要将using 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

Finally, if you know that your collection will only have a certain number of things, you can use an array instead.最后,如果你知道你的集合只有一定数量的东西,你可以使用数组代替。 Your code will now be:您的代码现在将是:

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

int[] numbers = new int[5]; //Creates an int array with 5 elements
//Meaning you can only access numbers[0] to numbers[4] inclusive
    numbers[4] = dice5;
    numbers[3] = dice4;
    numbers[2] = dice3;
    numbers[1] = dice2;
    numbers[0] = dice1;

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

Avoid using array list避免使用数组列表

Use List<int> or int[]使用List<int>int[]

Then the objects contained are typed instead of being object然后包含的对象被键入而不是对象

您可以在一行中完成:

List<int> numbers = new List<int>{ 4, 3, 6, 4, 5 };

You need to parse the object to string then to int value then use it with * operator.But you firstly have to initialize the arraylist with null values then assign number values So that, use following code that I made clear changes for you.您需要将对象解析为字符串,然后解析为 int 值,然后将其与 * 运算符一起使用。

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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM