您现在的位置是:首页 >

二维数组实现打印输出 c#简单实现二维数组和二维数组列表List的转置

火烧 2022-05-15 11:42:50 1039
c#简单实现二维数组和二维数组列表Li t的转置   刚看到网上一篇博文里用 ql实现了行列转置 ql erver / 只用一个 ivot函数就可以实现 ql erver 很多行的复杂实现 提到转置

c#简单实现二维数组和二维数组列表List的转置  

  刚看到网上一篇博文里用sql实现了行列转置 sql server / 只用一个pivot函数就可以实现sql server 很多行的复杂实现 提到转置 立刻想起还在求学阶段曾经做过的一个练习 用c语言实现二维数组的转置 相信大家都做过这个练习 下面利用c#利器也实现一遍 没有实际意义 练练手而已

   二维数组转置

  Code

  class Program

  {

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

  {

  int x = array GetUpperBound( ); //一维

  int y = array GetUpperBound( ); //二维

  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] = i ToString() + j ToString();

  }

  }

  //显示原数组

  Console WriteLine( Source Array: );

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

  {

  string soureResult = string Empty;

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

  {

  soureResult += array[i j] +   ;

  }

  Console WriteLine(soureResult);

  }

  string[ ] newArray = Rotate(array);

  //显示转置后的数组

  Console WriteLine( Destiney Array: );

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

  {

  string dstResult = string Empty;

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

  {

  dstResult += newArray[i j] +   ;

  }

  Console WriteLine(dstResult);

  }

  Console ReadLine();

  }

  }

   二维数组列表List<>的转置

  这个是想到在实际项目中操作集合经常用到泛型List 顺便实现一下它的转置 思路很简单 根据 我们已经实现了转置 所以就要想方设法把泛型List转换成二维数组 然后转置 接着将转换后的数组再转换成泛型List 呵呵 笔者偷懒惯了 其实应该还有其他的方法 不管了 先实现看下效果

  Code

  class Program

  {

  /// <summary>

  /// 二维数组转置函数

  /// </summary>

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

  /// <returns></returns>

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

二维数组实现打印输出 c#简单实现二维数组和二维数组列表List的转置

  {

  int x = array GetUpperBound( ); //一维

  int y = array GetUpperBound( ); //二维

  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 = original ToArray();

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

  if (array Length== )

  {

  throw new IndexOutOfRangeException( Index OutOf Range );

  }

  int x = array[ ] Count;

  int y = original Count;

  //将列表抓换成数组

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

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

  {

  int j = ;

  foreach (string s in array[i])

  {

  oArray[i j] = s;

  j++;

  }

  }

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

  newTwoArray = Rotate(oArray);//转置

  //二维数组转换成二维List集合

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

  {

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

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

  {

  list Add(newTwoArray[i j]);

  }

  lists Add(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++)

  {

  list Add(i ToString() + j ToString());

  }

  sourceList Add(list);

  }

  //显示原列表

  Console WriteLine( Source List: );

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

  {

  string soureResult = string Empty;

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

  {

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

  }

  Console WriteLine(soureResult);

  }

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

  //显示转置后的列表

  Console WriteLine( Destiney List: );

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

  {

  string dstResult = string Empty;

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

  {

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

  }

  Console WriteLine(dstResult);

  }

  Console ReadLine();

  }

lishixinzhi/Article/program/net/201311/11311  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码