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

时间:2017-04-16 03:02:15

标签: c# list mono

好的,这是缩短的代码,改编自我之前提到的程序,完全擦除了这篇文章的内容。滚动到底部,默认输出也会被注释掉:

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 修复了骨折错误,因为我不知道如何阅读。

1 个答案:

答案 0 :(得分:1)

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

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