using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; namespace 二维数组
{
class Program
{
static void Main(string[] args)
{
int row = ;//行
int col = ;//lie
FileStream fs;
string path = @"C:\Documents and Settings\Administrator\桌面\1.txt";
fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr;
sr = new StreamReader(fs);
List<double[]> arrary = new List<double[]>(); while (!sr.EndOfStream)
{
string[] arr = sr.ReadLine().Split(' ');
col = arr.Length;
double[] temp=new double[col];
for (int x = ; x < col; x++)
{
temp[x] = Convert.ToDouble(arr[x]);
}
arrary.Add(temp);
for (int y = ; y < col; y++)
{
Console.Write(arrary[row][y] + " ");
}
row++;
Console.WriteLine("\n");
}
sr.Close();
fs.Close();
Console.Read();
}
}
}
arrary定义的泛型 double数组类型。是二维数组的使用方法。可以通过arrary[x][y],进行数据的读取 .注意必须先add 每次add一次 ,x就加1.
如果arrary这样定义
List<double[,]> arrary = new List<double[,]>(); 那么读取数据应该这样arrary[num][x,y] 每次add一次 num都会+1, add赋值时也必须是[,]的对象。 注意:.net中有一些非托管对象有close和dispose两个方法,最安全的做法是先close 再 dispose。 后来啊 一看还有好多更简单的做法了 你瞧:
public short[] readFile(string fileName)
{
string[] dt = File.ReadAllLines(fileName);
short[] b = new short[dt.Length];
for (int i = ; i < b.Length; i++)
{
b[i] = short.Parse(dt[i]);
}
return b;
}
这就好了 ,简单方便 多好