从文本文件c#二维数组 [英] 2d Array from text file c#

查看:110
本文介绍了从文本文件c#二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文本文件

  1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
 

在一个10×10的网格。 使用C# 我需要的文本文件,并把它变成一个整数的二维数组,这样我可以操控整数一个独立的水平。请帮忙着做出来,

解决方案

 字符串输入= File.ReadAllText(@C:\ myfile.txt的);

INT I = 0,J = 0;
INT [,]结果=新的INT [10,10]。
的foreach(VAR行input.Split('\ N'))
{
    J = 0;
    的foreach(在row.Trim()。斯普利特VAR COL(''))
    {
        结果[I,J] = int.Parse(col.Trim());
        J ++;
    }
    我++;
}
 

该指数将 0为主因此,如果您要访问的 10 在列第四的行:

  Console.WriteLine(结果[3,9]); // 40
 

I have a text file that looks like this

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

in a 10 x 10 grid. using c# I need to take the text file and turn it into a 2d array of integers so that I can manipulate the integers on an independent level. Please help cant work it out,

解决方案

String input = File.ReadAllText( @"c:\myfile.txt" );

int i = 0, j = 0;
int[,] result = new int[10, 10];
foreach (var row in input.Split('\n'))
{
    j = 0;
    foreach (var col in row.Trim().Split(' '))
    {
        result[i, j] = int.Parse(col.Trim());
        j++;
    }
    i++;
}

The indices will be 0-based so if you want to access 10th column in fourth row:

Console.WriteLine(result[3,9]); //40

这篇关于从文本文件c#二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆