《C#4.0程序设计与项目实战》——C# 中的数组

数组的几种声明方式:
数组类型[] 数组名=new 数组类型[大小];
数组类型[,,] 数组名=new 数组类型[大小];//多维数组
int[,] array=new int[3,2] { {1,2},{3,4},{5,6}};
数组类型[][] 数组名=new 数组类型[大小][大小];//多维数组也可以理解为每个元素本身是一个数组的以为数组,这种情况下数组又称为交错数组。交错数组的声明方式如下
int[][] array=new int[3][];
int[][] array=new int[][]{new int[]{2,4,6},new int[] {1,3,5,7,9}};
不使用关键字new例如int [] num={1,2,3,4};

遍历数组:
使用foreach语句遍历数组;
使用for循环遍历数组;
使用Ienumerator接口遍历数组
int[] n=new int[4] {2,4,6,8};
Ienumerator eed=n.GetEnumerator();
while(eed.MoveNext())
{
  Console.WriteLine(eed.Current.ToString());
}

Array类:
在C#中,声明的数组在后台会被创建成一个派生自抽象类Array的新类,这样数组就可以使用Array类为每个C#数组定义的方法和属性。
排序Array.Sort(Array);
反转Array.Reverse(Array);//反转一个一维数组中的元素顺序
更多方法作用详见http://msdn.microsoft.com/zh-cn/library/vstudio/system.array.aspx

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample11_3
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义数组
            int[] array = new int[] { 34, 56, 343, 678, 78, 90, 34, 23 };
            Console.WriteLine("数组array:");
            //遍历数组
            foreach (int i in array)
            {
                System.Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            Console.WriteLine("使用方法BinarySearch搜索数组");
            Console.WriteLine("{0}在数组array中的序号是:{1}", 678, Array.BinarySearch(array, 678));
            Console.WriteLine("使用方法Find搜索数组");
            Console.WriteLine("数组array中满足搜索条件的第一项是:{0}", Array.Find(array, FindContion));
            Console.WriteLine("使用方法FindAll搜索数组");
            Console.Write("数组array中满足搜索条件的所有项是:");
            int[] findAll = Array.FindAll(array, FindContion);
            foreach (int i in findAll)
            {
                System.Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            Console.WriteLine("使用方法FindLast搜索数组");
            Console.WriteLine("数组array中满足搜索条件的最后一项是:{0}", Array.FindLast(array, FindContion));
            Console.WriteLine("使用方法IndexOf搜索数组");
            Console.WriteLine("{0}在数组array中的序号是:{1}", 34, Array.IndexOf(array, 34));
            Console.WriteLine("使用方法LastIndexOf搜索数组");
            Console.WriteLine("{0}在数组array中的序号是:{1}", 34, Array.LastIndexOf(array, 34));
            Console.ReadLine();

        }
        //方法Find、FindAll和FindLast的搜索条件
        private static bool FindContion(int num)
        {
            if (num > 100)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample11_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] {34,56,343,678,78,90,34,23};
            Console.WriteLine("排序前:");
            foreach (int i in array)
            {
                System.Console.Write("{0} ", i);
            }
            Console.WriteLine("");
            Console.WriteLine("排序后:");
            Array.Sort(array);//调用方法Sort进行排序
            foreach (int i in array)
            {
                System.Console.Write("{0} ", i);
            }
            Console.WriteLine("");
            Console.WriteLine("反转数组顺序:");
            Array.Reverse(array);//调用方法Reverse反转数组顺序
            foreach (int i in array)
            {
                System.Console.Write("{0} ", i);
            }
            Console.ReadLine();
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;



namespace Sample11_4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] Student = new string[,] {{"John","90"},{"Jim","98"},{"Tom","78"},{"Steve","80"} };
            string[,] StudentCopy = new string[,] { { "Lily", "96" }, { "LiLei", "89" }, { "Wangyang", "56" },
            { "Unix", "70" },{ "Lunix", "77" } };
            Console.WriteLine("源数组:");
            PrintArray(Student);
            Console.WriteLine("");
            Console.WriteLine("目标数组:");
            PrintArray(StudentCopy);
            Console.WriteLine("");
            Console.WriteLine("复制后目标数组:");
            Array.Copy(Student, StudentCopy,4);//复制
            PrintArray(StudentCopy);
            
            Console.ReadLine();
        }

        //打印数组
        static void PrintArray(string[,] array)
        {
            foreach (string s in array)
            {
                Console.Write(s + " ");
            }
        }
    }
}
posted @ 2012-11-17 15:58  .NET~莫愁  阅读(150)  评论(0编辑  收藏  举报