简体   繁体   English

如何使用 foreach 填充数组?

[英]How do I populate an array with foreach?

How do I get the data I can write in the console to write to the array and the console.如何获取可以在控制台中写入的数据以写入数组和控制台。

At the moment it only displays on the console (not added functionality to add to array).目前它只显示在控制台上(没有添加到阵列的功能)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace TBParser
{
   class Program
   {
    static void Main(string[] args)
    {
        String[] arr = new String[100];
        string[] lines = System.IO.File.ReadAllLines(@"C:\ShpereCompare3.txt");
        Console.WriteLine("Contents of Text File: ");
        foreach (string line in lines)
        {
            Console.WriteLine("\r\t" + line);

        }
        System.IO.File.WriteAllLines(@"C:\Test.txt",lines);
        Console.WriteLine("Press any key to Exit");
        Console.ReadKey();    
    }
   }
}

if my lines of text say如果我的文字行说

hello
my
name
is
Simon

then the first 5 slots of the array should contain each line?那么数组的前 5 个插槽应该包含每一行吗?

The line:线路:

string[] lines = System.IO.File.ReadAllLines(@"C:\ShpereCompare3.txt");

is already creating an array, each element of which contains one line.已经在创建一个数组,其中的每个元素都包含一行。

There is no need to populate a new array with this same information via a foreach .无需通过foreach用相同的信息填充新数组。

If you want to copy the lines from the text file into another array, then you can do this:如果要将文本文件中的行复制到另一个数组中,则可以执行以下操作:

String[] arr = new String[lines.Length];
Array.Copy(lines, arr, lines.Length);

found a work around.找到了解决办法。 the path i was going down was too complicated.我要走的路太复杂了。 thanks for all your input感谢您的输入

fixed code here固定代码在这里

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace TBParser
{
   class Program
   {
    static void Main(string[] args)
    {
        string fileName = @"C:shpereCompare3.txt";
        List<string> Names = new List<string>();
        List<string> Value = new List<string>();
        using (StreamReader fileReader = new StreamReader(fileName))
        {
            string fileLine;

            while (!fileReader.EndOfStream)
            {
                fileLine = fileReader.ReadLine();
                if (fileLine.StartsWith("Name"))
                {
                    Names.Add(fileLine.Substring(21));
                }
                if (fileLine.StartsWith("Center"))
                {
                    string[] fileSplit = fileLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    Value.Add(fileSplit[1]);
                }
            }
            string outputString = "";
            for (int i = 0;i < Names.Count; i++)
            {
                outputString += Names[i] + " = " + Value[i] + "\r\n";
            }
            System.IO.File.WriteAllText(@"C:Test.txt", outputString);
        }
    }
}

} }

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

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