Here is my code: An ArrayList of ArrayList that returns a float:
public ArrayList walls=new ArrayList();
public void Start()
{
walls[0
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:
float a= (float)((ArrayList)paredes[i])[0];
A better solution though is to use generics and populate a List<float> instead:
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:
float a = paredes[i][0];
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>>();
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];
}
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.
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:
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>
If you just want to make the code work without moving from ArrayList to List, you need an additional cast:
float a = (float)((ArrayList)paredes[i])[0];
But it's a lot cleaner just to use List<float> instead.