C# 无法将 [] 索引应用于“对象”类型的表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14004442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot apply indexing with [] to an expression of type `object'
提问by JamesB
Here is my code: An ArrayList of ArrayList that returns a float:
这是我的代码:返回浮点数的 ArrayList 的 ArrayList:
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;
}
It gives me the following error:
它给了我以下错误:
error CS0021: Cannot apply indexing with [] to an expression of type `object'
错误 CS0021:无法将 [] 索引应用于“对象”类型的表达式
I already did the casting, what is wrong? :(
我已经进行了选角,有什么问题吗?:(
采纳答案by Lee
The problem is that paredes[i]
returns an object
which is the return type of the ArrayList
indexer. You need to cast this to an ArrayList
to be able to access it:
问题在于paredes[i]
返回的object
是ArrayList
索引器的返回类型。您需要将此转换为 anArrayList
才能访问它:
float a= (float)((ArrayList)paredes[i])[0];
A better solution though is to use generics and populate a List<float>
instead:
更好的解决方案是使用泛型并填充 a List<float>
:
List<float> RetornaEmList(float a,float b,float c, float d, float e)
{
return new List<float> { a, b, c, d, e };
}
then paredes
is a List<List<float>>
and your accessor can be changed to:
然后paredes
是 aList<List<float>>
并且您的访问器可以更改为:
float a = paredes[i][0];
回答by CSharper
Where are you doing the casting?
你在哪里选角?
I would say it must be (did not try with a compiler):
我会说它必须是(没有尝试使用编译器):
for (int i = 0; i < paredes.Length; i++)
{
float a=(float)((ArrayList)paredes[i])[0];
...
}
Did you consider using the generic collections instead?
您是否考虑过使用泛型集合?
public List<List<float>> paredes = new List<List<float>>();
回答by Scott Chapman
ArrayList
stores objects without limiting what type those objects are. When you access the objects stored in an ArrayList
, the compiler doesn't know what type they are, so it just gives you type object
.
ArrayList
存储对象而不限制这些对象的类型。当您访问存储在 an 中的对象时ArrayList
,编译器不知道它们是什么类型,因此它只为您提供 type object
。
You're storing an ArrayList
of float
in your outer ArrayList
. Since you're always storing floats, it would be better to use a List<float>
for the inner list, and a List<List<float>>
for the outer list. This way you won't have to type cast from object
:
您将ArrayList
of存储float
在您的外部ArrayList
. 由于您总是存储浮点数,因此最好将 aList<float>
用于内部列表,而 aList<List<float>>
用于外部列表。这样你就不必输入 cast from 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 };
}
Since the inner list always has 5 floats, you could also use a float[]
instead of a List<float>
由于内部列表总是有 5 个浮点数,您也可以使用 afloat[]
而不是 aList<float>
If you just want to make the code work without moving from ArrayList
to List
, you need an additional cast:
如果您只想使代码工作而不从ArrayList
to移动List
,则需要额外的演员阵容:
float a = (float)((ArrayList)paredes[i])[0];
But it's a lot cleaner just to use List<float>
instead.
但只是使用它更干净List<float>
。
回答by Kundan Singh Chouhan
Code should be like this :
代码应该是这样的:
//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];
}