Triple-nested List<> in C#
I asked a question not too long ago about multi-dimensional and jagged arrays in C#. Well, I was trying the List class instead and was running into a bit of a jam. What happens is only a few of the values in wtflist actually make it to the end of Main(). How can I make sure all of the data gets in (and stays in) wtflist?
Edit: Here is my NEW code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleIometerParser
{
class Program
{
public static int linenum = 0;
public static int totaldisks = 0;
public static int disk = 0;
public static List<List<List<string>>> overallList = new List<List<List<string>>>();
static int Main(string[] args)
{
string filename = args[0];
string[] lines = System.IO.File.ReadAllLines(@filename);
System.Console.WriteLine("Processing Data...");
foreach (string line in lines)
{
string[] linearray;
string lineNoQuotes;
char[] delimiter = new Char[] { ',' };
lineNoQuotes = line.Replace("\"", string.Empty);
lineNoQuotes = lineNoQuotes.Replace("'", string.Empty);
linearray = lineNoQuotes.Split(delimiter);
totaldisks = (linearray.Length - 1) / 10;
for (disk = 0; disk < totaldisks; disk++)
{
List<List<string>> diskDataList = new List<List<string>>();
List<string> dataList = new List<string>();
dataList.Add("");
dataList.Add(args[1]);
dataList.Add(linearray[0]);
for (int i = 1; i < 10; i++)
{
int arraypos = disk + (i * totaldisks);
dataList.Add(linearray[arraypos].ToString());
diskDataList.Add(dataList);
}
overallList.Add(diskDataList);
}
linenum++;
}
int count1 = 0;
int count2 = 0;
int count3 = 0;
foreach (var wtf in overallList)
{
foreach (var lol in wtf)
{
foreach (var zomg in lol)
{
Console.WriteLine("overallList[{0},{1},{2}]: {3}", count1, count2, count3, zomg);
count3++;
}
count3 = 0;
count2++;
}
count2 = 0;
count1++;
}
Console.WriteLine("Data Processed Successfully.");
Console.Read();
return 0;
}
}
}
New Output:
Processing Data...
overallList[0,0,0]:
overallList[0,0,1]: 1
overallList[0,0,2]: Column1
overallList[0,0,3]: Column4
overallList[0,0,4]: Column7
overallList[0,0,5]: Column10
overallList[0,0,6]: Column13
overallList[0,0,7]: Column16
overallList[0,0,8]: Column19
overallList[0,0,9]: Column22
overallList[0,0,10]: Column25
overallList[0,0,11]: Column28
overallList[0,1,0]:
overallList[0,1,1]: 1
overallList[0,1,2]: Column1
overallList[0,1,3]: Column5
overallList[0,1,4]: Column8
overallList[0,1,5]: Column11
overallList[0,1,6]: Column14
overallList[0,1,7]: Column17
overallList[0,1,8]: Column20
overallList[0,1,9]: Column23
overallList[0,1,10]: Column26
overallList[0,1,11]: Column29
overallList[0,2,0]:
overallList[0,2,1]: 1
overallList[0,2,2]: Column1
overallList[0,2,3]: Column6
overallList[0,2,4]: Column9
overallList[0,2,5]: Column12
overallList[0,2,6]: Column15
overallList[0,2,7]: Column18
overallList[0,2,8]: Column21
overallList[0,2,9]: Column24
overallList[0,2,10]: Column27
overallList[0,2,11]: Column30
overallList[1,0,0]:
overallList[1,0,1]: 1
overallList[1,0,2]: 07/08/10 03:04 PM
overallList[1,0,3]:
overallList[1,0,4]:
overallList[1,0,5]:
overallList[1,0,6]: 0
overallList[1,0,7]:
overallList[1,0,8]:
overallList[1,0,9]:
overallList[1,0,10]:
overallList[1,0,11]:
overallList[1,1,0]:
overallList[1,1,1]: 1
overallList[1,1,2]: 07/08/10 03:04 PM
overallList[1,1,3]:
overallList[1,1,4]:
overallList[1,1,5]: 0
overallList[1,1,6]:
overallList[1,1,7]:
overallList[1,1,8]:
overallList[1,1,9]:
overallList[1,1,10]:
overallList[1,1,11]:
overallList[1,2,0]:
overallList[1,2,1]: 1
overallList[1,2,2]: 07/08/10 03:04 PM
overallList[1,2,3]:
overallList[1,2,4]:
overallList[1,2,5]: 0
overallList[1,2,6]:
overallList[1,2,7]:
overallList[1,2,8]:
overallList[1,2,9]:
overallList[1,2,10]:
overallList[1,2,11]:
overallList[2,0,0]:
overallList[2,0,1]: 1
overallList[2,0,2]: 07/08/10 03:06 PM
overallList[2,0,3]: 98.6
overallList[2,0,4]: 0.02
overallList[2,0,5]: 0.02
overallList[2,0,6]: 0
overallList[2,0,7]: 508500.98
overallList[2,0,8]: 70684.4
overallList[2,0,9]: 10.91
overallList[2,0,10]: 30.66
overallList[2,0,11]: 437816.59
overallList[2,1,0]:
overallList[2,1,1]: 1
overallList[2,1,2]: 07/08/10 03:06 PM
overallList[2,1,3]: 0.02
overallList[2,1,4]: 0.02
overallList[2,1,5]: 0
overallList[2,1,6]: 501532.1
overallList[2,1,7]: 70684.4
overallList[2,1,8]: 10.91
overallList[2,1,9]: 29.84
overallList[2,1,10]: 430847.7
overallList[2,1,11]: 18.93
overallList[2,2,0]:
overallList[2,2,1]: 1
overallList[2,2,2]: 07/08/10 03:06 PM
overallList[2,2,3]: 0
overallList[2,2,4]: 0
overallList[2,2,5]: 0
overallList[2,2,6]: 6968.88
overallList[2,2,7]: 0
overallList[2,2,8]: 0
overallList[2,2,9]: 0.81
overallList[2,2,10]: 6968.88
overallList[2,2,11]: 0.81
overallList[3,0,0]:
overallList[3,0,1]: 1
overallList[3,0,2]: 07/08/10 03:07 PM
overallList[3,0,3]: 99.86
overallList[3,0,4]: 0
overallList[3,0,5]: 0
overallList[3,0,6]: 0
overallList[3,0,7]: 84469.57
overallList[3,0,8]: 273.07
overallList[3,0,9]: 0.07
overallList[3,0,10]: 6.14
overallList[3,0,11]: 84196.5
overallList[3,1,0]:
overallList[3,1,1]: 1
overallList[3,1,2]: 07/08/10 03:07 PM
overallList[3,1,3]: 0
overallList[3,1,4]: 0
overallList[3,1,5]: 0
overallList[3,1,6]: 78092.25
overallList[3,1,7]: 273.07
overallList[3,1,8]: 0.07
overallList[3,1,9]: 5.39
overallList[3,1,10]: 77819.18
overallList[3,1,11]: 5.32
overallList[3,2,0]:
overallList[3,2,1]: 1
overallList[3,2,2]: 07/08/10 03:07 PM
overallList[3,2,3]: 0
overallList[3,2,4]: 0
overallList[3,2,5]: 0
overallList[3,2,6]: 6377.32
overallList[3,2,7]: 0
overallList[3,2,8]: 0
overallList[3,2,9]: 0.76
overallList[3,2,10]: 6377.32
overallList[3,2,11]: 0.76
overallList[4,0,0]:
overallList[4,0,1]: 1
overallList[4,0,2]: 07/08/10 03:09 PM
overallList[4,0,3]: 99.83
overallList[4,0,4]: 0
overallList[4,0,5]: 0
overallList[4,0,6]: 0
overallList[4,0,7]: 35277.63
overallList[4,0,8]: 1849.23
overallList[4,0,9]: 0.32
overallList[4,0,10]: 5.1
overallList[4,0,11]: 33428.4
overallList[4,1,0]:
overallList[4,1,1]: 1
overallList[4,1,2]: 07/08/10 03:09 PM
overallList[4,1,3]: 0
overallList[4,1,4]: 0
overallList[4,1,5]: 0
overallList[4,1,6]: 25764.05
overallList[4,1,7]: 1576.11
overallList[4,1,8]: 0.29
overallList[4,1,9]: 4.05
overallList[4,1,10]: 24187.94
overallList[4,1,11]: 3.76
overallList[4,2,0]:
overallList[4,2,1]: 1
overallList[4,2,2]: 07/08/10 03:09 PM
overallList[4,2,3]: 0
overallList[4,2,4]: 0
overallList[4,2,5]: 0
overallList[4,2,6]: 9513.58
overallList[4,2,7]: 273.12
overallList[4,2,8]: 0.03
overallList[4,2,9]: 1.06
overallList[4,2,10]: 9240.46
overallList[4,2,11]: 1.02
overallList[5,0,0]:
overallList[5,0,1]: 1
overallList[5,0,2]: 07/08/10 03:10 PM
overallList[5,0,3]: 99.91
overallList[5,0,4]: 0
overallList[5,0,5]: 0
overallList[5,0,6]: 0
overallList[5,0,7]: 34662.41
overallList[5,0,8]: 182.04
overallList[5,0,9]: 0.03
overallList[5,0,10]: 4.92
overallList[5,0,11]: 34480.36
overallList[5,1,0]:
overallList[5,1,1]: 1
overallList[5,1,2]: 07/08/10 03:10 PM
overallList[5,1,3]: 0
overallList[5,1,4]: 0
overallList[5,1,5]: 0
overallList[5,1,6]: 25691.03
overallList[5,1,7]: 91.02
overallList[5,1,8]: 0.02
overallList[5,1,9]: 4.06
overallList[5,1,10]: 25600.01
overallList[5,1,11]: 4.03
overallList[5,2,0]:
overallList[5,2,1]: 1
overallList[5,2,2]: 07/08/10 03:10 PM
overallList[5,2,3]: 4.67E-005
overallList[5,2,4]: 0
overallList[5,2,5]: 0
overallLi开发者_如何学编程st[5,2,6]: 8971.38
overallList[5,2,7]: 91.02
overallList[5,2,8]: 0.01
overallList[5,2,9]: 0.87
overallList[5,2,10]: 8880.36
overallList[5,2,11]: 0.86
Data Processed Successfully.
Here's the OLD code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleIometerParser
{
class Program
{
public static int linenum = 0;
public static int totaldisks = 0;
public static int disk = 0;
public static List<List<List<string>>> wtflist = new List<List<List<string>>>();
//public static string[,,] sqlarray;
static int Main(string[] args)
{
string filename = args[0];
string[] lines = System.IO.File.ReadAllLines(@filename);
System.Console.WriteLine("Processing Data...");
foreach (string line in lines)
{
//Console.WriteLine("Linenum: " + linenum);
string[] linearray;
string lineNoQuotes;
char[] delimiter = new Char[] { ',' };
lineNoQuotes = line.Replace("\"", string.Empty);
lineNoQuotes = lineNoQuotes.Replace("'", string.Empty);
linearray = lineNoQuotes.Split(delimiter);
totaldisks = (linearray.Length - 1) / 10;
//sqlarray = new string[totaldisks, lines.Length, 13];
for (disk = 0; disk < totaldisks; disk++)
{
List<List<string>> lollist = new List<List<string>>();
//sqlarray[disk, linenum, 0] = ""; //blank field for the SQL id field
//sqlarray[disk, linenum, 1] = args[1]; //testid
//sqlarray[disk, linenum, 2] = linearray[0]; //time
for (int i = 1; i < 10; i++)
{
List<string> zomglist = new List<string>();
int arraypos = disk + (i * totaldisks);
//sqlarray[disk, linenum, i + 2] = linearray[arraypos].ToString();
//Console.WriteLine("sqlarray[{0},{1},{2}]: {3}", disk, linenum, i, sqlarray[disk, linenum, i]);
zomglist.Add(linearray[arraypos].ToString());
Console.WriteLine("zomglist data: {0}", zomglist.Last());
if (i == 9) lollist.Add(zomglist);
}
wtflist.Add(lollist);
}
linenum++;
}
foreach (var wtf in wtflist)
{
foreach (var lol in wtf)
{
foreach (var zomg in lol)
{
Console.WriteLine("ZOMG: " + zomg);
}
}
}
Console.WriteLine("Data Processed Successfully.");
Console.Read();
return 0;
}
}
}
Your line: if (i == 9) lollist.Add(zomglist);
means that only 1/10th (or every 10th) of the zomglist
s created will get added to the lollist
. Looks like it's executing perfectly to me.
You are creating a new zomglist every time through the for i loop, but only adding it to lollist when i == 9. The other 9 zomglists are lost.
But it seems that the purpose of the for i loop is to fill the zomglist, so you really need to move the create of zomglist & it addition to lollist to outside the loop:
List<string> zomglist = new List<string>();
for (int i = 1; i < 10; i++)
{
int arraypos = disk + (i * totaldisks);
zomglist.Add(linearray[arraypos].ToString());
Console.WriteLine("zomglist data: {0}", zomglist.Last());
}
lollist.Add(zomglist);
Similarly, the creation of lollist & it's addition to wtflist needs to be moved outside the for disk loop.
You have this:
for (int i = 1; i < 10; i++)
{
List<string> zomglist = new List<string>();
which will create a new zomglist
every time through the loop. I think you want to have a single zomglist
that you always operate on, like this:
List<string> zomglist = new List<string>();
for (int i = 1; i < 10; i++)
{
精彩评论