How to access and read an arraylist within an Arraylist?

I am trying to access an arraylist within an Arraylist but the arraylist within the arraylist is returned as an object. How can I open the imbedded arraylist and read the values of that Arraylist?

It’s generally better to use Lists instead of ArrayLists. Here is the solution with Lists:

List<List<Foo>> listList = new List<List<Foo>>();


listList[2][4].stuff = 0;

However, if you absolutely need to keep it as an ArrayList, you can cast the values of the ArrayList to another ArrayList:

ArrayList listList = new ArrayList();


((ArrayList)listList[0])[0].ToString(); //this is just a simple Object

((GameObject)((ArrayList)listList[0])[0]).SetActive(true); //you can then cast that Object to whatever else you want

As you can see, it’s easier to use Lists for nesting because you can lose track of which ArrayList value Object should be what; casting incorrectly will create an error.