zoukankan      html  css  js  c++  java
  • Array Reserve

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SurfaceQuestion.Grammar
    {
        
    public class ArrayReverse
        {
            
    public ArrayReverse()
            {
                
    int[] a = new int[] { 123456 };
                
    this.Way1(a);
                Console.WriteLine(
    "_____________________");

                
    this.Way2(a);
                Console.WriteLine(
    "_____________________");

                
    this.Way3(a);
            }

            
    /// <summary>
            
    /// 新数组
            
    /// </summary>
            private void Way1(int[] a)
            {
                
    int[] b = new int[a.Length];
                
    for (int i = 0; i < a.Length; i++)
                {
                    Console.WriteLine(
    "count:"+i);
                    Console.Write(a[i]);
                    b[i] 
    = a[a.Length - 1 - i];
                }

                Console.WriteLine();
                
    foreach (var item in b)
                {
                    Console.Write(item);
                }
            }

            
    /// <summary>
            
    /// 临时变量
            
    /// </summary>
            
    /// <param name="a"></param>
            private void Way2(int[] a)
            {
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }

                
    int temp = 0;
                
    for (int i = 0; i < a.Length/2; i++)
                {
                    Console.WriteLine(
    "count:" + i);

                    temp 
    = a[i];
                    a[i] 
    = a[a.Length - 1 - i];
                    a[a.Length 
    - 1 - i] = temp;
                }

                Console.WriteLine();
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }
            }

            
    /// <summary>
            
    /// while
            
    /// mscorlib实现方式
            
    /// </summary>
            
    /// <param name="a"></param>
            private void Way3(int[] a)
            {
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }

                
    int num = 0;
                
    int num2 = a.Length - num - 1;
                
    while (num2 > num)
                {
                    Console.WriteLine(
    "count:" + num);

                    
    int temp = a[num];
                    a[num] 
    = a[num2];
                    a[num2] 
    = temp;
                    num
    ++;
                    num2
    --;
                }

                Console.WriteLine();
                
    foreach (var item in a)
                {
                    Console.Write(item);
                }
            }
        }
    }


  • 相关阅读:
    平台
    重构之践
    Linux.NET
    系统分析员级下午试题II(论文)解答方法
    通用泛型存储接口的设计
    .NET平台4.0 发布网站流程及出错总结
    在IIS上发布基于Windows Azure Service Bus的WCF服务
    epoll + 多线程实现并发网络连接处理
    Linux进程地址空间之初探:一
    排序、搜索
  • 原文地址:https://www.cnblogs.com/chinaniit/p/1718871.html
Copyright © 2011-2022 走看看