C# 字符串列表:防止两次打印相同的索引

C# List of Strings: Prevent Printing the Same Index Twice

好的,这就是我之前提到的完全擦除此post内容的程序改编的缩短的代码。滚动到底部,还有为您注释掉的默认输出:

using System;
using System.Collections;
using System.Collections.Generic;
namespace StackExchange {
    class MainClass {
        List<String> inputs = new List<String> ();
        public String line;

        public static String file = "This is a file";

        public static String choice1 = "Determine where the tracks go";

        public static String[] hw = {
            "Value 1",
            "Value 3"
        };

        public static String[] rgb = {
            "Value 1",
            "Value 2", 
            "Value 3"
        }; 

        public static void Main(string[] args) {
            MainClass main = new MainClass();

            main.inputs.Add(file);
            main.inputs.AddRange (hw);
            main.inputs.Add(choice1);
            main.inputs.AddRange (rgb);

            foreach (string value in main.inputs) { 
                //Console.Write (Text.inputs.IndexOf(value));
                main.line = "|     " + main.inputs.IndexOf(value) + "     |     " + value;
                Console.WriteLine (main.line);
            }
        }
    }
}

        // The output when the program gets run:

        //|     0     |     This is a file
        //|     1     |     Value 1
        //|     2     |     Value 3
        //|     3     |     Determine where the tracks go
        //|     1     |     Value 1
        //|     5     |     Value 2
        //|     2     |     Value 3

现在,将其粘贴并编译到您喜欢用来编译 C# 程序的任何 IDE 或记事本中,它会按照描述运行。

而不是:

        //|     0     |     This is a file
        //|     1     |     Value 1
        //|     2     |     Value 3
        //|     3     |     Determine where the tracks go
        //|     1     |     Value 1
        //|     5     |     Value 2
        //|     2     |     Value 3

如何让左侧像这样上升 0-7:

        //|     0     |     This is a file
        //|     1     |     Value 1
        //|     2     |     Value 3
        //|     3     |     Determine where the tracks go
        //|     4     |     Value 1
        //|     5     |     Value 2
        //|     6     |     Value 3

感谢您在此时提供的任何帮助。


编辑 1

所以,我是根据下面@MetaColon 的回复得出的结论。我想要做的是让程序像我的第一个例子一样并排显示索引和元素。在此示例中,我得到@MetaColon 的响应与原始响应的一部分并排工作。

谁能嫁给下面这两个段子?

using System;
using System.Collections;
using System.Collections.Generic;
namespace StackExchange {
    class MainClass {
        List<String> inputs = new List<String> ();
        public String line;

        public static String file = "This is a file";

        public static String choice1 = "Determine where the tracks go";

        public static String[] hw = {
            "Value 1",
            "Value 3"
        };

        public static String[] rgb = {
            "Value 1",
            "Value 2", 
            "Value 3"
        }; 

        static string value;


        public static void Main(string[] args) {
            MainClass main = new MainClass();

            main.inputs.Add(file);
            main.inputs.AddRange (hw);
            main.inputs.Add(choice1);
            main.inputs.AddRange (rgb);

                for (int i = 0; i < main.inputs.Count; i++)
                {
                    main.line = $"|     {i}     |     {value}";
                    Console.WriteLine(main.line);
                }

                foreach (String value in main.inputs) { 
                    main.line = "     |     " + value;
                    Console.WriteLine (main.line);
                }
            }
        }
    }

    // The output when the program gets run:

    //|     0     |     
    //|     1     |     
    //|     2     |     
    //|     3     |     
    //|     4     |     
    //|     5     |     
    //|     6     |     
    //     |     This is a file
    //     |     Value 1
    //     |     Value 3
    //     |     Determine where the tracks go
    //     |     Value 1
    //     |     Value 2
    //     |     Value 3

编辑 2 修复了愚蠢的错误,因为我显然不知道如何阅读。

您的问题是您有多个同名项目(例如 Value 1、Value3)。所以 IndexOf 方法 returns 找到元素的第一个索引。因此,对于 Value 1 有两个索引,一个索引为 1,一个索引为 4。虽然 IndexOf returns 最低索引,但您想要更高的索引在这种情况下。您可以做的是使用 for 循环或添加索引。对于这种情况,我会使用 for 循环:

//...
for (var i = 0; i < main.inputs.Count; i++)
{
    value = main.inputs[i];
    main.line = $"|     {i}     |     {value}";
    Console.WriteLine(main.line);
}
//...