I’m trying to have nested ArrayLists, and tried to add a new ArrayList as an element of an existing one, but it doesn’t seem to work. Here’s the code:
private void readDataFile()
{
// Open stream reader
StreamReader reader = new StreamReader(dataFileLoc);
string line = string.Empty;
line = reader.ReadLine();
myArgus.Add(new ArrayList().Add(line));
while ((line = reader.ReadLine()) != null)
{
// TODO
}
// Close the reader
reader.Close();
}
If I add a breakpoint and look at the data in myArgus (the already existing ArrayList), the first element is just 0. It should be an ArrayList which has the first element as 10016 (that’s the first line of the text file).
What am I doing wrong here?
Thanks!
Edit: It’s worth noting that we are required to use an ArrayList.
ArrayList.Add does not return ArrayList, but simply int.
So you want to add elements in 2 steps.
Note: Use strongly typed collections (
List<string>) to avoid this type of errors.