c#语言d数组转置,C#二维数组文件流读写并转置

C#二维数组文件流读写并转置

C#二维数组文件流读写并转置

C#二维数组的文件流读写并进行转置

链接: link.

图片:

ecf9797e4cdfe7b5895a487342e12381.png进行转置后的效果

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

namespace 二维数组读写进文件流

{

class Program

{

public static int ROW = 4;

public static int COL = 4;

static int[,] arrayA = new int[ROW, COL];

static void Main(string[] args)

{

Write(); //把二维数组写入文件

string[,] strArray = Read(); //读出文件流的二维数组

ArrayNN(ref strArray, ROW, COL);

Console.ReadKey();

}

static void ArrayNN(ref string[,] a, int row, int col)

{

string[,] tmp = new string[row, col];

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)

{

tmp[i, row - j - 1] = a[j, i];

}

}

Console.Write("\n文件流转置之后:");

for (int i = 0; i < row; i++)

{

Console.WriteLine();

for (int j = 0; j < col; j++)

{

Console.Write("{0}" + "\t", tmp[i, j]);

}

}

}

public static string[,] Read()

{

string[,] str = new string[ROW, COL];

string[] a;

StreamReader sr = new StreamReader(@"D:\write.txt", Encoding.Default);

Console.Write("文件流读取出来的数组:\n");

for (int i = 0; i < ROW; i++)

{

String line = sr.ReadLine();

a = line.Split('\t');

for (int j = 0; j < COL; j++)

{

str[i, j] = a[j];

Console.Write(str[i, j]+"\t");

}

Console.WriteLine();

}

return str;

}

public static void Write()

{

//int[,] arrayA = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };

int[,] arrayA = new int[ROW, COL];

FileStream fs = new FileStream(@"D:\write.txt", FileMode.Create);

StreamWriter sw = new StreamWriter(fs);

Random rd = new Random();

for (int i = 0; i < ROW; i++)

{

for (int j = 0; j < COL; j++)

{

arrayA[i, j] = rd.Next(1, 100);

sw.Write(arrayA[i,j]+"\t");

}

sw.WriteLine();

}

Console.WriteLine("写入完成");

//清空缓冲区

sw.Flush();

//关闭流

sw.Close();

fs.Close();

}

}

}

C#二维数组文件流读写并转置相关教程