- Home /
Array of Array
I have been looking around for a while now but have found nothing which has fixed problem. So i'm asking you. I want to be able to store an Array of strings into an array. I have no idea how to go about this so if you can help I would be extremely grateful.
PS. I'm using unity script
Answer by Mapleman · Apr 20, 2015 at 05:17 AM
You can't create an array of string arrays with .NET. At first, this might sound like nonsense, but all elements in array must be of the same type. Array of strings is clearly different type than just plain string. Put I assume that you want to create multidimensional string array, or perhaps a list of string arrays. Check code below:
public class StringTester
{
private string[,] TwoDimensionalArray;
private string[][] JaggedArray;
private string[] OneDimensionalArray;
private List<string[]> ListOfArrays;
public void Test()
{
//Two dimensional array of strings
TwoDimensionalArray = new string[10,10];
TwoDimensionalArray[2, 2] = "Hello World";
//Two dimensional jagged array (variable length arrays)
JaggedArray = new string[5][];
JaggedArray[0] = new string[2];
JaggedArray[1] = new string[6];
JaggedArray[0][0] = "Foo";
JaggedArray[1][1] = "Bar";
//List of string arrays
ListOfArrays = new List<string[]>();
ListOfArrays.Add(new string[10]);
ListOfArrays.Add(new string[8]);
ListOfArrays[0][0] = "Some text";
}
}
What you said is not true, array of strings is a type just like any other. Indeed you can have an array of arrays of strings. That makes every element in the array to be of same type (array of strings). Anyway it's not true that all elements in the array have to be of same type. This is true for value types which are final and cannot be derived from. For any other classes you can indeed have an array that contains objects of different types that all derive from the same specified class.
Let's start with some basic things about arrays. Arrays are in fact blocks of memory reserved from the heap. When we want to access some element i from array, compiler actually addresses it like [Array_Start_address_in_heap]+[Element_size]*i.
In case of string arrays, elements in array are actually references to some other memory addresses in heap. And same goes to all other reference types. Arrays never hold the object as they are, only references to them. In case of value types, the values are directly stored in same continuos block of memory.
From these comes the fact the so called array of string of arrays just isn't possible. It would mean a structure like 'Array of refenrences to arrays of strings'. It becomes even more clear why this kind of construct isn't supported when you think if our array elements would be of some custom struct type (structs are value-types). So that would mean that we should have a construct where we would need to have array of references (4 or 8 bytes per element) where each elements points to an array of structs where each element can be any arbitrary size. So clearly the memory addressing wouldn't work.
But this was more of academic interest only. For the one who asked the original question, I think most flexible way would be using list of string arrays, or perhaps even list of list of strings.