将文本文件的数据存储到数组中 [英] Storing data of text file into arrays

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

问题描述

我在这里有一个问题,



我有一个两列的文本文件,最多2000行,文本文件看起来像这样。

实际上,我在文本文件中记录了数据



1 1

2 2

3 3

4 4

::

::

::

100 100



我想将每列的数据存储到两个不同的数组中。

数组是固定长度的,只存储读取文本文件时有10个值。一旦10个值被填充,他们的数组就会用10个新闻值更新它们。



我尝试了什么:



我不知道怎么做请帮助

I have a question here,

I have a text file of two columns, and up to 2000 rows and text file look like this.
actually, I logged the data in the text file

1 1
2 2
3 3
4 4
: :
: :
: :
100 100

I want to store the data of each column into two different arrays.
The arrays are of fixed length is it stores only 10 values while reading the text file. Once the 10 values get filled they arrays updates them with 10 news values.

What I have tried:

I don know how to do it please help

推荐答案

这看起来像是作业,所以我不会给你代码!

但这很简单:

读取行 - 你可以用几种方式做到这一点,但最简单的可能是使用 File.ReadAllLines 返回一个数组,每个文本行都在一个单独的数组中。

然后处理每一行。由于您只对最后十项感兴趣,请使用数组的长度属性来查找最后十个项目的开始位置。

每行,使用 string.Split 将其分成两个数字,并使用 int.TryParse 转换每个部分数字值。

将它们存储在你的两个数组中。



很简单,如果你逐步采取它 - 它可能更少总共十行代码!
This looks like homework, so I'll give you no code!
But it's very simple:
read the lines - you can do this in several ways, but the easiest is probably to use File.ReadAllLines which returns an array with each line of text in a separate array.
Then process each line. Since you are only interested in the last ten items, use the Length property of the array to find where the last ten starts.
For each line, use string.Split to break it into the two numbers, and use int.TryParse to convert each of the parts into a numeric value.
Store these in your two arrays.

Easy enough, if you take it stage by stage - it's probably less than ten lines of code in total!


这篇关于将文本文件的数据存储到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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