Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文本文件中的行存储到固定大小的数组中_C#_Arrays - Fatal编程技术网

C# 如何将文本文件中的行存储到固定大小的数组中

C# 如何将文本文件中的行存储到固定大小的数组中,c#,arrays,C#,Arrays,所以,我有一个场景,我从一个结构如下的文件中读取: 4 4 8 2 1 2 4 6 3 3 6 9 2 1 3 2 8 class Program { static void Main(string[] args) { var fileName = "sampleinput.txt"; int T = Convert.ToInt32(File.ReadLines(fileName).First()); // gets the first lin

所以,我有一个场景,我从一个结构如下的文件中读取:

4
4 8 2 1
2 4 6 3
3 6 9 2
1 3 2 8
 class Program
{
    static void Main(string[] args)
    {
        var fileName = "sampleinput.txt";
        int T = Convert.ToInt32(File.ReadLines(fileName).First()); // gets the first line from file.
        var lines = File.ReadLines(fileName).Skip(1).Take(T);
        int[,] array1 = new int[T, T];
        foreach(var line in lines)
        {
            string[] lineElements = line.Split(" ");
            int j=0;
            for(int i=0;i<lineElements.Length;i++)
            {
               array1[j,i] = Convert.ToInt32(lineElements[i]);
            }
            j++;
        }

    }
}
其中第一行中的
4
是将生成的矩阵的大小。 下面几行是矩阵值,它始终是一个多维矩阵,大小为
[4,4]
,表示它将有4行4列

如何将其存储在数据结构中并访问元素以执行计算

我当前的代码:

class Program
{
    static void Main(string[] args)
    {
        var fileName = "sampleinput.txt";
        int T = Convert.ToInt32(File.ReadLines(fileName).First()); // gets the first line from file.
        var lines = File.ReadLines(fileName).Skip(1).Take(T);
        int[,] array1 = new int[T, T];
        foreach(var line in lines)
        {

            Console.WriteLine(line);
            //How to store the values here in a multidimensional array and access the values as required?
        }

    }
}
试试这个:

class Program
{
static void Main(string[] args)
{
    var fileName = "sampleinput.txt";
    int T = Convert.ToInt32(File.ReadLines(fileName).First()); // gets the first line from file.
    var lines = File.ReadLines(fileName).Skip(1).Take(T);
    int[,] array1 = new int[T, T];
    var n = 0
    foreach(var line in lines)
    {
       var j = 0; 
       foreach(var i in line.Split().Select(s => int.Parse(s))) 
       { 
        array1 [n, j] = i;
        j++; 
       }
      n++;
    }

}
}

这似乎是在学习如何操作字符串和如何使用数据结构。在foreach循环中,您将从变量行中的文件中获得一行。因此,您可以拆分此字符串,然后将其存储到字符串数组中,然后在foreach循环中启动一个新循环,该循环将遍历此字符串数组,并将值分配给多维数组,如下所示:

4
4 8 2 1
2 4 6 3
3 6 9 2
1 3 2 8
 class Program
{
    static void Main(string[] args)
    {
        var fileName = "sampleinput.txt";
        int T = Convert.ToInt32(File.ReadLines(fileName).First()); // gets the first line from file.
        var lines = File.ReadLines(fileName).Skip(1).Take(T);
        int[,] array1 = new int[T, T];
        foreach(var line in lines)
        {
            string[] lineElements = line.Split(" ");
            int j=0;
            for(int i=0;i<lineElements.Length;i++)
            {
               array1[j,i] = Convert.ToInt32(lineElements[i]);
            }
            j++;
        }

    }
}
类程序
{
静态void Main(字符串[]参数)
{
var fileName=“sampleinput.txt”;
int T=Convert.ToInt32(File.ReadLines(fileName.First());//从文件中获取第一行。
var lines=File.ReadLines(文件名).Skip(1).Take(T);
int[,]array1=新的int[T,T];
foreach(行中的var行)
{
string[]lineElements=line.Split(“”);
int j=0;

对于(int i=0;这看起来像是一个家庭作业。很乐意帮忙,但你至少需要先尝试一下。哈哈哈!也许采访@kevin你可以从
string[]number=line.Split(“”);