为什么这个列表只打印出最后一个字符?

时间:2016-10-02 09:19:55

标签: c#

所以我一直致力于这个分析某个输入文件的程序,几乎所有东西都输出正确,除了关键字List。

下面是关于关键字列表和相关功能的代码段:

public class Token //Token structure to be added for each Token in the tokenTable
{
    public string value;
    public string type;
}

internal static class Define_input_output
{
    public const string inFile = "input.txt"; //set file to read
    public const string outFile = "output.txt"; //set file to write to
}

public static void placeKeywords_in_keywordsTable(List<string> keywords, List<Token> keywordTable)
{
    Token newToken = new Token();

    for (int i = 0; i < keywords.Count; i++) //loop through each keyword
    {
        newToken.value = keywords[i]; //add keyword[i] to newToken's value
        newToken.type = "KW"; //assign "kw" to newToken's type
        keywordTable.Add(newToken); //push newToken to keywordTable
    }
}

public static void print_Tables_to_outputFile(StreamWriter output, List<Token> keywordTable)
{
    output.Write("\r\n"); 
    output.Write("-".PadRight(21, '-'));
    output.Write("\r\n"); 
    output.Write(' ');
    output.Write("Keyword ".PadRight(11));
    output.Write("|".PadRight(2));
    output.Write("Index");
    output.Write("\r\n");
    output.Write("-".PadRight(21, '-'));
    output.Write("\r\n");

    for (int i = 0; i < keywordTable.Count; i++) //loop through each element in keywordTable
    {
        output.Write(' ');
        output.Write((keywordTable[i].value).PadRight(10));
        output.Write(" |".PadRight(7));
        output.Write(i);
        output.Write("\r\n");
    }
}

static int Main()
{
    StreamReader input = new StreamReader(Define_input_output.inFile);//input file
    StreamWriter output = new StreamWriter(Define_input_output.outFile);//output file

    List<string> keywords = new List<string>() { "else", "if", "int", "return", "void", "while", "+", "-", "*", "/", "<", "<=", ">", ">=", "==", "!=", "=", ";", ",", "(", ")", "[", "]", "{", "}", "/*", "*/" };
    List<Token> keywordTable = new List<Token>();

    placeKeywords_in_keywordsTable(keywords, keywordTable); //place all the keywords into keywordTable

    while (!input.EndOfStream) //read each line until end of file
    {
        read_inputFile(input, output, keywordTable);
    }

    print_Tables_to_outputFile(output, keywordTable); 

    input.Close(); //close input file
    output.Close(); //close output file

    return 0;
}

我遇到的问题只有这个&#34; * /&#34;字符正在打印到关键字表。它的索引是26次(这是正确的索引量,因为列表中有多少个字符)。 我通过从列表中删除最后两个字符来测试这个,并且&#34;}&#34;字符开始打印26次。

我不知道丢失了什么因为我多次仔细查看代码而我无法发现问题所在。 任何帮助表示赞赏。谢谢!

keywordTable output image

1 个答案:

答案 0 :(得分:4)

问题出在您的placeKeywords_in_keywordsTable方法中。您只需初始化Token一次,然后在循环的每次迭代中更新它。所以基本上只留下最后keyword

更改为:

for (int i = 0; i < keywords.Count; i++) //loop through each keyword
{
    Token newToken = new Token();
    newToken.value = keywords[i]; //add keyword[i] to newToken's value
    newToken.type = "KW"; //assign "kw" to newToken's type
    keywordTable.Add(newToken); //push newToken to keywordTable
}

更好的方法是使用这种初始化形式:

for (int i = 0; i < keywords.Count; i++) //loop through each keyword
{
    Token newToken = new Token
    {
        value = keywords[i],
        type = "KW"
    };
    keywordTable.Add(newToken); //push newToken to keywordTable
}

更好的方法是使用foreach循环代替for

foreach(var i in keywords)
{
    Token newToken = new Token
    {
        value = i,
        type = "KW"
    };
    keywordTable.Add(newToken);
}

如果你想使用linq:

keywordTable = keywords.Select(item => new Token { value = item, type = "KW" }).ToList();