简体   繁体   English

将字符串添加到List <string[]> 名单

[英]Add strings to List<string[]> list

I'm new to C#, I need to do the following: 我是C#的新手,我需要做以下事情:

I need to declare a List 我需要声明一个List

List<string[]> myList = new List<string[]>();

Now the conditions and adding to the list should work as follows (in pseudo code): 现在条件和添加到列表应该如下(在伪代码中):

for int i = 0 to N
if nNumber == 1 add string[i] to myList[0]
else to myList[1]

In the end, I would have 2 members in my list, each member having several strings. 最后,我的列表中有2个成员,每个成员都有几个字符串。

I am not sure how I can add these strings to the members as the method List(Of T).Add inserts members to the list and I do not want this. 我不知道如何将这些字符串添加到成员中作为方法List(Of T).Add将成员插入到列表中,我不希望这样。

From what I can see, you don't want a list of string arrays, you want a 2-element array of lists of strings: 从我所看到的,你不需要一个字符串数组列表,你想要一个2元素的字符串列表数组:

var myList = new List<string>[] { new List<string>(), new List<string>() };

Now you should be able to implement the algorithm exactly as you specified. 现在,您应该能够完全按照指定的方式实现算法。

        var myList = new List<string>[] { new List<string>(), new List<string>() }; 

        for (int i = 0; i <= 12; i++)
        {
            if (i == 1)
                myList.Last().Add(i.ToString());
            else
                myList.First().Add(i.ToString());
        }

This can be also performed as List of List of string as below: 这也可以作为字符串列表执行如下:

    List<List<string>> strList = new List<List<string>>();
    for (int i = 0; i < N; i++)
    {
        if (nNumber == 1)
            strList[0].Add(i.ToString());
        else
            strList[1].Add(i.ToString());
    }

In my csv parsing class, I have the following defined 在我的csv解析类中,我定义了以下内容

enum gic_rpt_columns { AGYDIV, STS, GICID, LASTNAME, FIRSTNAME, COVERAGE, DESCRIPTION , PREMIUM, RUNDATE, BILMO, EMPLOYERPERCENT };

public class csv_parser
{
    .
    .
    .
    private string[]        m_column_headings;
    List<string[]>          m_csv_array = new List<string[]>();
    .
    .
    .

The following code will add my column headings and then the rows in the csv file 以下代码将添加我的列标题,然后添加csv文件中的行

               using (TextFieldParser parser = new TextFieldParser(m_csv_path))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");

                    if (!parser.EndOfData)
                    {
                        m_column_headings = parser.ReadFields();
                        m_csv_array.Add(m_column_headings);
                    }

                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        m_csv_array.Add(fields);
                        m_row_count++;
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM