剛看到網上一篇博文裡用sql實現了行列轉置
Code
class Program
{
public static string[
{
int x = array
int y = array
string[
for (int i =
{
for (int j =
{
newArray[j
}
}
return newArray;
}
static void Main(string[] args)
{
string[
for (int i =
{
for (int j =
{
array[i
}
}
//顯示原數組
Console
for (int i =
{
string soureResult = string
for (int j =
{
soureResult += array[i
}
Console
}
string[
//顯示轉置後的數組
Console
for (int i =
{
string dstResult = string
for (int j =
{
dstResult += newArray[i
}
Console
}
Console
}
}
這個是想到在實際項目中操作集合經常用到泛型List
Code
class Program
{
/// <summary>
/// 二維數組轉置函數
/// </summary>
/// <param name=
/// <returns></returns>
public static string[
{
int x = array
int y = array
string[
for (int i =
{
for (int j =
{
newArray[j
}
}
return newArray;
}
/// <summary>
/// 將二維列表(List)轉換成二維數組
/// </summary>
/// <param name=
/// <returns></returns>
public static List<List<string>> Rotate(List<List<string>> original)
{
List<string>[] array = original
List<List<string>> lists = new List<List<string>>();
if (array
{
throw new IndexOutOfRangeException(
}
int x = array[
int y = original
//將列表抓換成數組
string[
for (int i =
{
int j =
foreach (string s in array[i])
{
twoArray[i
j++;
}
}
string[
newTwoArray = Rotate(twoArray);//轉置
//二維數組轉換成二維List集合
for (int i =
{
List<string> list = new List<string>();
for (int j =
{
list
}
lists
}
return lists;
}
static void Main(string[] args)
{
List<List<string>> sourceList = new List<List<string>>(); //測試的二維List
for (int i =
{
List<string> list = new List<string>();
for (int j =
{
list
}
sourceList
}
//顯示原列表
Console
for (int i =
{
string soureResult = string
for (int j =
{
soureResult += sourceList[i][j] +
}
Console
}
List<List<string>> dstList = Rotate(sourceList);
//顯示轉置後的列表
Console
for (int i =
{
string dstResult = string
for (int j =
{
dstResult += dstList[i][j] +
}
Console
}
Console
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11311.html