在C#中,二維數(shù)組可以與其他數(shù)據(jù)結(jié)構(gòu)(如列表、字典等)進(jìn)行轉(zhuǎn)換
int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
List<List<int>> list = new List<List<int>>();
for (int i = 0; i< array.GetLength(0); i++)
{
List<int> innerList = new List<int>();
for (int j = 0; j< array.GetLength(1); j++)
{
innerList.Add(array[i, j]);
}
list.Add(innerList);
}
List<List<int>> list = new List<List<int>>()
{
new List<int> { 1, 2 },
new List<int> { 3, 4 },
new List<int> { 5, 6 }
};
int[,] array = new int[list.Count, list[0].Count];
for (int i = 0; i< list.Count; i++)
{
for (int j = 0; j< list[i].Count; j++)
{
array[i, j] = list[i][j];
}
}
int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
Dictionary<int, int> dictionary = new Dictionary<int, int>();
for (int i = 0; i< array.GetLength(0); i++)
{
for (int j = 0; j< array.GetLength(1); j++)
{
dictionary[i * array.GetLength(1) + j] = array[i, j];
}
}
Dictionary<int, int> dictionary = new Dictionary<int, int>()
{
{ 0, 1 },
{ 1, 2 },
{ 2, 3 },
{ 3, 4 },
{ 4, 5 },
{ 5, 6 }
};
int rows = (int)Math.Sqrt(dictionary.Count);
int cols = dictionary.Count / rows;
int[,] array = new int[rows, cols];
for (int i = 0; i< rows; i++)
{
for (int j = 0; j< cols; j++)
{
array[i, j] = dictionary[i * cols + j];
}
}
這些示例展示了如何在C#中將二維數(shù)組與其他數(shù)據(jù)結(jié)構(gòu)進(jìn)行轉(zhuǎn)換。請(qǐng)根據(jù)實(shí)際需求調(diào)整代碼。
億速云公眾號(hào)
手機(jī)網(wǎng)站二維碼
Copyright ? Yisu Cloud Ltd. All Rights Reserved. 2018 版權(quán)所有
廣州億速云計(jì)算有限公司粵ICP備17096448號(hào)-1 粵公網(wǎng)安備 44010402001142號(hào)增值電信業(yè)務(wù)經(jīng)營(yíng)許可證編號(hào):B1-20181529