List> stuff = new List>(); stuff.Add(new List()); stuff[0].Add("hello"); Console.WriteLine(stuff[0][0]);
Manip wrote:
ArrayList stuff = new ArrayList(); stuff.Add(new ArrayList()); ((ArrayList)stuff[0]).Add("Hello World"); MessageBox.Show((string)((ArrayList)stuff[0])[0]); |
That's nothing compared to defining and initializing a multidimensional vector in C++!
std::vector<std::vector<int> > stuff(5, std::vector<int>(5, 42));<BR>
Which creates a 5x5 matrix with the value 42 in every position.
Multi-dimensional arrays are much easier to work with in most languages, and usually more efficient too (if you're doing a lot of sequential scans, otherwise the difference is negligable) because they're strided instead of jagged like the vector-in-a-vector (or ArrayList-in-an-ArrayList) type structures.
The nice thing about generics though is that it makes the syntax slightly more managable by getting rid of the casts. In C# 2.0 your example becomes:
List<List<string>> stuff = new List<List<string>>();<BR>stuff.Add(new List<string>());<BR>stuff[0].Add("hello");<BR>Console.WriteLine(stuff[0][0]);And fortunately, unlike C++, C# doesn't require the stupid space between the two closing >.