无法将带有[]的索引应用于类型为`object'的表达式

时间:2012-12-22 16:21:19

标签: c# object arraylist indexing

这是我的代码:ArrayList的ArrayList,它返回一个float:

public ArrayList paredes=new ArrayList();   

public void Start()
{
    paredes[0] = RetornaEmArrayList(279,275,0,0,90);
    paredes[1] = RetornaEmArrayList(62,275,0,0,0);
    paredes[2] = RetornaEmArrayList(62,275,62,0,90);
    paredes[3] = RetornaEmArrayList(217,275,62,-62,0);
    paredes[4] = RetornaEmArrayList(62,275,279,0,90);
    paredes[5] = RetornaEmArrayList(41,275,279,0,0);
    paredes[6] = RetornaEmArrayList(279,275,320,0,9);
    paredes[7] = RetornaEmArrayList(320,275,0,-279,0);  

    for (int i = 0; i < paredes.Length; i++) {
        float a = (float)paredes[i][0];
        float b = (float)paredes[i][1];
        float c = (float)paredes[i][2];
        float d = (float)paredes[i][3];
        float e = (float)paredes[i][4];
    }
}

ArrayList RetornaEmArrayList(float a,float b,float c, float d, float e)
{
    ArrayList arrayList = new ArrayList();
    arrayList.Add(a);
    arrayList.Add(b);
    arrayList.Add(c);
    arrayList.Add(d);
    arrayList.Add(e);
    return arrayList;
}

它给了我以下错误:

  

错误CS0021:无法将带有[]的索引应用于类型的表达式   `对象

我已经做了演员,有什么不对? :(

4 个答案:

答案 0 :(得分:9)

问题是paredes[i]返回object,它是ArrayList索引器的返回类型。您需要将其转换为ArrayList才能访问它:

float a= (float)((ArrayList)paredes[i])[0];

更好的解决方案是使用泛型并填充List<float>代替:

List<float> RetornaEmList(float a,float b,float c, float d, float e)
{
    return new List<float> { a, b, c, d, e };
}

然后paredesList<List<float>>,您的访问者可以更改为:

float a = paredes[i][0];

答案 1 :(得分:2)

ArrayList存储对象而不限制这些对象的类型。当您访问存储在ArrayList中的对象时,编译器不知道它们是什么类型,因此它只是为您提供object类型。

您在外ArrayList内存储了float ArrayListList<float>。由于您总是存储浮点数,因此最好使用List<List<float>>作为内部列表,并使用object作为外部列表。这样您就不必从using System.Collections.Generic; public List<List<float>> paredes = new List<List<float>>(); Start() { paredes[0]=RetornaEmList(279,275,0,0,90); paredes[1]=RetornaEmList(62,275,0,0,0); paredes[2]=RetornaEmList(62,275,62,0,90); paredes[3]=RetornaEmList(217,275,62,-62,0); paredes[4]=RetornaEmList(62,275,279,0,90); paredes[5]=RetornaEmList(41,275,279,0,0); paredes[6]=RetornaEmList(279,275,320,0,9); paredes[7]=RetornaEmList(320,275,0,-279,0); for (int i=0;i<paredes.Length;i++) { float a=paredes[i][0]; float b=paredes[i][1]; float c=paredes[i][2]; float d=paredes[i][3]; float e=paredes[i][4]; } } List<float> RetornaEmList(float a,float b,float c, float d, float e) { return new List<float> { a, b, c, d, e }; } 输入强制转换:

float[]

由于内部列表总是有5个浮点数,您还可以使用List<float>而不是ArrayList

如果您只是希望在不从List移动到float a = (float)((ArrayList)paredes[i])[0]; 的情况下使代码正常工作,则需要额外的演员:

List<float>

但是使用{{1}}代替它会更加清晰。

答案 2 :(得分:1)

你在哪里做演员?

我会说它一定是(没有尝试使用编译器):

for (int i = 0; i < paredes.Length; i++)
{           
    float a=(float)((ArrayList)paredes[i])[0];
    ...                   
}

您是否考虑过使用通用集合?

public List<List<float>> paredes = new List<List<float>>();   

答案 3 :(得分:0)

代码应该是这样的:

//Adding List

paredes.Add(RetornaEmArrayList(279,275,0,0,90));
paredes.Add(RetornaEmArrayList(62,275,0,0,0));
paredes.Add(RetornaEmArrayList(62,275,62,0,90));
paredes.Add(RetornaEmArrayList(217,275,62,-62,0));
paredes.Add(RetornaEmArrayList(62,275,279,0,90));
paredes.Add(RetornaEmArrayList(41,275,279,0,0));
paredes.Add(RetornaEmArrayList(279,275,320,0,9));
paredes.Add(RetornaEmArrayList(320,275,0,-279,0));


//Traversing List

foreach(ArrayList item in paredes)
{
    float a=(float)item[0];
    float b=(float)item[1];
    float c=(float)item[2];
    float d=(float)item[3];
    float e=(float)item[4];
}