数组的应用 【把唐诗格式的转换成古代对联格式的(在右边从上往下读)】

首先在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;

        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值