C#旅途 ----趁热打铁 活学活用 数组快排,查找,拷贝;

需要掌握的部分语法:

1.数组的定义:(这里和C不同的是,先括号,后变量名字)

<strong>(1)         int[] Inum;
             string[] Inum;2.</strong></strong>
<strong>(2)        int[] Inum = new int[10];</strong></strong>

<strong>(3)        int[] Inum; Inum = new int[10];
</strong>

2.输入(用for循环+convert类输入);

3.处理数组的类:Array类

   (1)sort:升序排序(PS:会把开范围的数组整体排序,所以会出错;所以按数组需要开)

   (2)IndexOf():查找首次出现元素位置;  LastIndexOf():查找最后一次元素出现位置;

   (3)Reverse:逆序

   (4)Copy:复制a数组到b数组;


(一)数组的快排

<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n,i;
            
            Console.WriteLine("请输入数组大小:");
            n = Convert.ToInt32(Console.ReadLine());
            int[] num = new int[n];///*按需求开数组
            Console.WriteLine("请输入n个数字:");
            for(i=0;i<n;i++)
            {
                num[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("未排序时的数组为:");
            for(i=0;i<n;i++)
            {
                Console.Write(num[i] + " ");
            }
            Array.Sort(num);//*排序
            Console.WriteLine();
            Console.WriteLine("排序后的数组为:");
            for (i = 0; i < n; i++)
            {
                Console.Write(num[i] + " ");
            }
            Console.ReadKey();
        }
    }
}
</strong>


(二)数组元素位置的查询:

<strong>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 查找1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("请输入数组大小");
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n+1];
            Console.WriteLine("请输入"+n+"个数");
            for( i =1 ; i<=n;i++)
            {
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("请输入需要查找的数");
            int b, c,search;
            search=Convert.ToInt32(Console.ReadLine());
            b = Array.IndexOf(a, search);
            Console.WriteLine("元素" + search + "首次出现的位置为:" + b);
            c = Array.LastIndexOf(a, search);
            Console.WriteLine("元素" + search + "最后一次出现的位置为:" + c);
            Console.ReadKey();
        }
    }
}
</strong>


(三)数组的逆序:PS(Reverse函数逆序后是从下标为0开始计数的)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 查找1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("请输入数组大小");
            int n;
            n = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[n];
            Console.WriteLine("请输入"+n+"个数");
            for( i =0; i<n;i++)
            {
                a[i]=Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("未逆序的数组为:");
            for(i=0; i<n;i++)
            {
                Console.Write(a[i]+" ");
            }
            Console.WriteLine();
            Console.WriteLine("逆序后的数组为:");
            Array.Reverse(a);
            for(i=0;i<n;i++)
            {
                Console.Write(a[i]+" ");
            }
            Console.ReadKey();
        }
    }
}






  




你可能感兴趣的:(C#旅途 ----趁热打铁 活学活用 数组快排,查找,拷贝;)