首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

三种C#实现数组反转模式

2013-09-12 
三种C#实现数组反转方式今天上班中午吃饱之后、逛博客溜达看到一道题:数组反转晚上回家洗完澡没事情做,就自

三种C#实现数组反转方式

今天上班中午吃饱之后、逛博客溜达看到一道题:数组反转  晚上回家洗完澡没事情做,就自己练习一把。

        /// <summary>        /// 使用自定义方法实现反转(使用栈《后进先出》)        /// </summary>        /// <param name="arr"></param>        /// <param name="begin"></param>        /// <param name="end"></param>        public static void ReverseDemo4(int[] arr, int begin, int end)        {            Console.WriteLine("使用自定义方法实现反转(使用栈《后进先出》)");            if (null == arr)            {                throw new ArgumentNullException("arr", "Array不能为null");            }            if (begin <= 0 || end <= 0)            {                throw new ArgumentOutOfRangeException("开始或结束索引没有正确设置");            }            if (end > arr.Length)            {                throw new ArgumentOutOfRangeException("end", "结束索引超出数组长度");            }           Stack<int> intStack = new Stack<int>();           int tempBegin = begin;            for(;begin<=end;begin++)            {                intStack.Push(arr[begin]);            }            for (; tempBegin <= end; tempBegin++)            {                arr[tempBegin] = intStack.Pop();            }        }    }


 

热点排行