[英]C# text file to 2d array?
如果我像這樣創建一個二維數組:
int[,] MyArray = new int[5, 5];
並有一個包含以下數字的文本文件:
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
2 3 4 6 7
7 8 9 6 4
如何將數字放入二維數組?
這應該很簡單。 嵌套循環是處理多維數組的“傳統”方法。
嵌套兩個循環,外部循環遍歷輸入中的行,內部循環遍歷一行。
string [] line = text.split(new char [] {'\\ n'},StringSplitOptions.RemoveEmptyEntries);
對於(int i = 0; i <line.Length; i ++)
{
string [] digit = text.Split(new char [] {''},StringSplitOptions.RemoveEmptyEntries);
對於(int j = 0; j <digit.Length; j ++)
{
MyArray [i,j] = Convert.ToInt32(digit [j]);
}
}
隨你怎么便...
基本上,您必須決定。
這是一種試探法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.