数组的应用 【把唐诗格式的转换成古代对联格式的(在右边从上往下读)】
来源:互联网 发布:sql入门视频教程 编辑:程序博客网 时间:2024/04/26 17:23
首先在vs2010中建个控制台应用程序
然后把下列代码放进去就行了
class Program
{
static void Main(string[] args)
{
List<List<string>> sourceList = new List<List<string>>(); //定义数组
for (int i = 0; i < 5; i++)
{
List<string> list = new List<string>();
for (int j = 0; j < 4; j++)
{
list.Add(i.ToString() + j.ToString());
}
sourceList.Add(list);
}
//显示原列表
List<List<string>> dstList = Rotate(sourceList);
Console.WriteLine("列表:");
for (int i = 0; i < dstList.Count; i++)
{
string dstResult = string.Empty;
for (int j = 0; j < dstList[i].Count; j++)
{
dstResult += dstList[i][j] + " ";
}
Console.WriteLine(dstResult);
}
//显示行转列后的列表
Console.WriteLine("行转列后的列表:");
for (int i = 0; i < sourceList.Count; i++)
{
string soureResult = string.Empty;
for (int j = 0; j < sourceList[i].Count; j++)
{
soureResult += sourceList[i][j] + " ";
}
Console.WriteLine(soureResult);
}
//显示结果的列表
Console.WriteLine("显示结果的列表:");
for (int i = 0; i < sourceList.Count; i++)
{
string soureResult = string.Empty;
for (int j = sourceList[i].Count; j > 0; j--)
{
soureResult += sourceList[i][j - 1] + " ";
}
Console.WriteLine(soureResult);
}
Console.ReadLine();
}
/// <summary>
/// 二维数组转置函数
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static string[,] Rotate(string[,] array)
{
int x = array.GetUpperBound(0); //一维
int y = array.GetUpperBound(1); //二维
string[,] newArray = new string[y + 1, x + 1]; //构造转置二维数组
for (int i = 0; i <= x; i++)
{
for (int j = 0; 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 == 0)
{
throw new IndexOutOfRangeException("Index OutOf Range");
}
int x = array[0].Count;
int y = original.Count;
//将列表抓换成数组
string[,] twoArray = new string[y, x];
for (int i = 0; i < y; i++)
{
int j = 0;
foreach (string s in array[i])
{
twoArray[i, j] = s;
j++;
}
}
string[,] newTwoArray = new string[x, y];
newTwoArray = Rotate(twoArray);//转置
//二维数组转换成二维List集合
for (int i = 0; i < x; i++)
{
List<string> list = new List<string>();
for (int j = 0; j < y; j++)
{
list.Add(newTwoArray[i, j]);
}
lists.Add(list);
}
return lists;
}
}
- 数组的应用 【把唐诗格式的转换成古代对联格式的(在右边从上往下读)】
- 怎么把图片上的文字转换成word格式
- 把VOB格式转换成其它格式的工具
- 如何把mo3格式的文件转换成mp3格式
- 把JPG图片转换成PDF格式的格式转换器
- 数组转换成nv12格式的mat格式
- 数组转换成json格式的数据
- 把库文件从msvc格式(.lib)转化成MinGW格式(.a)的方法。
- 把库文件从msvc格式(.lib)转化成MinGW格式(.a)的方法
- 把pdf转换成html格式的转换方法分享
- 把txt转换成pdf格式的转换方法
- 把pdf转换成word格式的转换方法
- 分享把word转换成pdf格式的转换方法
- json格式转换成Map的应用
- Android 从web应用获取xml格式的视频资讯并列表显示在屏幕上
- Android 从web应用获取json格式的视频资讯并列表显示在屏幕上
- 把ppt格式的文档转换为pdf格式
- Android 把YUV420P格式的数据转换为RGB565格式
- Python(7) 编写规范 pep8 的问题笔记
- PE文件到内存的映射
- Android应用开发中的风格和主题(style,themes)
- 利用NSRunLoop阻塞NSOperation线程
- 领域模型、贫血模型、充血模型概念总结
- 数组的应用 【把唐诗格式的转换成古代对联格式的(在右边从上往下读)】
- 对数据库中的数据做简单的——增删改查
- 25个必须记住的SSH命令
- NJUST 喵助理照夜景
- openlayers学习之Map
- 视频基础知识
- 如何上传自己的project到github
- python入门
- 过滤器解决Struts2中的中文乱码问题