熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#簡單實現二維數組和二維數組列表List<>的轉置

2022-06-13   來源: .NET編程 

  剛看到網上一篇博文裡用sql實現了行列轉置sql server /只用一個pivot函數就可以實現sql server 很多行的復雜實現提到轉置立刻想起還在求學階段曾經做過的一個練習用c語言實現二維數組的轉置相信大家都做過這個練習下面利用c#利器也實現一遍沒有實際意義練練手而已

  二維數組轉置

  Code

  class Program

  {

  public static string[] Rotate(string[] array)

  {

  int x = arrayGetUpperBound(); //一維

  int y = arrayGetUpperBound(); //二維

  string[] newArray = new string[y + x + ]; //構造轉置二維數組

  for (int i = ; i <= x; i++)

  {

  for (int j = ; j <= y; j++)

  {

  newArray[j i] = array[i j];

  }

  }

  return newArray;

  }

  static void Main(string[] args)

  {

  string[] array = new string[ ];

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

  {

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

  {

  array[i j] = iToString() + jToString();

  }

  }

  //顯示原數組

  ConsoleWriteLine(Source Array:);

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

  {

  string soureResult = stringEmpty;

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

  {

  soureResult += array[i j] +   ;

  }

  ConsoleWriteLine(soureResult);

  }

  string[] newArray = Rotate(array);

  //顯示轉置後的數組

  ConsoleWriteLine(Destiney Array:);

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

  {

  string dstResult = stringEmpty;

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

  {

  dstResult += newArray[i j] +   ;

  }

  ConsoleWriteLine(dstResult);

  }

  ConsoleReadLine();

  }

  }

  二維數組列表List<>的轉置

  這個是想到在實際項目中操作集合經常用到泛型List順便實現一下它的轉置思路很簡單根據我們已經實現了轉置所以就要想方設法把泛型List轉換成二維數組然後轉置接著將轉換後的數組再轉換成泛型List呵呵筆者偷懶慣了其實應該還有其他的方法不管了先實現看下效果

  Code

  class Program

  {

  /// <summary>

  /// 二維數組轉置函數

  /// </summary>

  /// <param name=array></param>

  /// <returns></returns>

  public static string[] Rotate(string[] array)

  {

  int x = arrayGetUpperBound(); //一維

  int y = arrayGetUpperBound(); //二維

  string[] newArray = new string[y + x + ]; //構造轉置二維數組

  for (int i = ; i <= x; i++)

  {

  for (int j = ; j <= y; j++)

  {

  newArray[j i] = array[i j];

  }

  }

  return newArray;

  }

  /// <summary>

  /// 將二維列表(List)轉換成二維數組二維數組轉置然後將二維數組轉換成列表

  /// </summary>

  /// <param name=original></param>

  /// <returns></returns>

  public static List<List<string>> Rotate(List<List<string>> original)

  {

  List<string>[] array = originalToArray();

  List<List<string>> lists = new List<List<string>>();

  if (arrayLength==)

  {

  throw new IndexOutOfRangeException(Index OutOf Range);

  }

  int x = array[]Count;

  int y = originalCount;

  //將列表抓換成數組

  string[] twoArray = new string[y x];

  for (int i = ; i < y; i++)

  {

  int j = ;

  foreach (string s in array[i])

  {

  twoArray[i j] = s;

  j++;

  }

  }

  string[] newTwoArray = new string[x y];

  newTwoArray = Rotate(twoArray);//轉置

  //二維數組轉換成二維List集合

  for (int i = ; i < x; i++)

  {

  List<string> list = new List<string>();

  for (int j = ; j < y; j++)

  {

  listAdd(newTwoArray[i j]);

  }

  listsAdd(list);

  }

  return lists;

  }

  static void Main(string[] args)

  {

  List<List<string>> sourceList = new List<List<string>>(); //測試的二維List

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

  {

  List<string> list = new List<string>();

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

  {

  listAdd(iToString() + jToString());

  }

  sourceListAdd(list);

  }

  //顯示原列表

  ConsoleWriteLine(Source List:);

  for (int i = ; i < sourceListCount; i++)

  {

  string soureResult = stringEmpty;

  for (int j = ; j < sourceList[i]Count; j++)

  {

  soureResult += sourceList[i][j] +   ;

  }

  ConsoleWriteLine(soureResult);

  }

  List<List<string>> dstList = Rotate(sourceList);

  //顯示轉置後的列表

  ConsoleWriteLine(Destiney List:);

  for (int i = ; i < dstListCount; i++)

  {

  string dstResult = stringEmpty;

  for (int j = ; j < dstList[i]Count; j++)

  {

  dstResult += dstList[i][j] +   ;

  }

  ConsoleWriteLine(dstResult);

  }

  ConsoleReadLine();

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/11311.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.