1.11M Members

multidimensional string Lists and arrays

 
0
 

I have a csv file (comma delimited) that contains an unknown number of columns, and unkown number of rows, and an unknown size of string for each element. Each row has the same number of elements however. I desire to read this data into a multidimensional array or List. Because I do not now the size a List seems most appropriate, but I am having difficulties.

I can read each line into a null string and separate the entries and create a single dimensional array for each row that is read in thus;

List<string> fileLines = File.ReadAllLines(strFileName).ToList();
int entries = fileLines.Count;
string str = String.Empty;
List<string> values = new List<string>();
for (i = 0; i < entries; i++){
     str = fileLines[i];//contains the value of the entire row in one string
     a = str.Split(',');//broken up into its elements.                              
     for (j=1; j<a.Length; j++){//need to eliminate the first element
          c = a[j];
          values.Add(c);//List that contains the elements of a that I need.
 }

Now I need to assign each 'value' to a row in a multidimensional list, but I cant figure out how to do that. It seems that sring arrays and Lists do not have the same method sets. The string array method set would work, but I would have to initialize the array size, and I cant do that because I dont know it.

 
0
 

This code uses the DataTable class, which I am not familiar with, but seems it could work for me. I was hoping to be able to use a read two dimensional List:

List<string[,]> table = new List<string[,]>();

where each row in the 2-D list contains a 1-D string List already obtained.

 
0
 

Well this line, which reads just one line of the file and drops all the string in a fieldValues array

string[] fieldValues = myReader.ReadLine().Split(new Char [] {seperator});

should give you some ideas, fieldValues.Length; tels you how many columns you have in your csv file.

 
0
 

As you say, because I know that the number of items in each row is constant, and I can count the number of rows and number of columns when I read in the file, I can create a multi-dimensional array thus;

string[,] c = new string[a,b];

And it works.

what doesnt work is a multi-dimensional List or a string array defined as;
string[][] c = new string[a];//not allowed even if a and b are known

any ideas why string[][] doesnt work and why a multidimensional List is such a hassle?

 
0
 

Your syntax is wrong. Don't worry I struggle with it all the time, but in the end it will work out.
Look here : http://www.csharpkey.com/csharp/arrays/Lesson04.htm to find out how to do it.

List<double>[,] myData = new List<double>[2,2];

would init a 2X2 array of doubles

 
0
 

Try,

List<List<string>> lst = new List<List<string>>();

            List<string> a = new List<string>() { "One", "Two" };
            List<string> b = new List<string>() { "Three", "Four", "Five" };

            lst.Add(a);
            lst.Add(b);

             //Iterate list
            foreach (var t in lst)
            {
                foreach (var ele in t)
                {

                     //code
                }
            }
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article