C#语言学习--基础部分(十六)数组(2)

五种数组Copy: ConsoleDemo

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

namespace ArrayDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] pins;
            pins = new int[4]{7,3,4,7};//两大特性:1.数组实例化完毕以后,定义下来数组元素的个数,数组元素的个数就不能改变.2.数组中每一个元素所存储的数值必须都是相同的数据类型.
            
//foreach (int s in pins)  
            
//{
            
//    Console.WriteLine(s);
            
//}
            for(int x=0;x<pins.Length;x++)
            {
                Console.WriteLine(pins[x]);
            }
            Console.WriteLine(pins[0]); //数组的访问
            Console.WriteLine(pins[1]);
            Console.WriteLine(pins[2]);
            Console.WriteLine(pins[3]);
            //pins = new int[3] { 7, 3, 4, 7 };//初始化长度不对应
            
//pins = new int[4] { 7, 3, 4 }; //初始化长度不对应
            
//int[] pins2;
            
//int i =int.Parse( Console.ReadLine());//动态指字数组维数
            
//pins2 = new int[i];
            int[] pins3 = { 9372 }; //编译器会默认认为数组元素的个数为4
           
//隐式数组类型
            var names = new[] { "aa""bb" };
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
            //var bad = new[] { "aa", "bb", 1, 2 };
            var numbers = new[] { 11223333.3345.35,23.9999999 };//double类型
            Console.WriteLine(numbers[numbers.Length -1]);
            //数组copy
            int []pins4=new int[]{7,3,5,2};
            int[] pins5;
            //地址copy
            pins5 = pins4; //此种赋值方式并不是把pins4数组里面每个元素都copy一份到pins5,这种赋值操作其实是把pins4这种整型数组的引用类型保存的地址copy到pins5中.
            foreach (int pin in pins5)
            {
                Console.WriteLine(pin);
            }
            pins4[1] = 100;
            Console.WriteLine(pins5[1]);
            //copy一个全新的数组
            int[] pins6 = new int[pins4.Length];
            for(int j=0;j<pins4.Length;j++)
            {
                pins6[j]=pins4[j];
            }
            pins4[1] = 200;
            Console.WriteLine(pins6[1]);
            //eg2
            string[] countrys = new string[] { "中国""日本""韩国""朝鲜" };
            string[] ctrycopy = new string[countrys.Length];
            countrys.CopyTo(ctrycopy, 0);//index从第几个元素开始copy,而且是copy到ctrycopy中第index个位置
            countrys[3] = "印度";
            foreach (string ctry in countrys)
            {
                Console.WriteLine(ctry);
            }
            Console.WriteLine("----------CopyTo------------");
            foreach (string cctry in ctrycopy)
            {
                Console.WriteLine(cctry);
            }
            Console.WriteLine("----------------------");
            //eg3 Array.Copy
            Array.Copy(countrys, ctrycopy, countrys.Length);
            countrys[1] = "美国";
            foreach (string ctry in countrys)
            {
                Console.WriteLine(ctry);
            }
            Console.WriteLine("----------Array.Copy-ctrycopy----------");
            foreach (string cctry in ctrycopy)
            {
                Console.WriteLine(cctry);
            }
            //eg4 
            string[] cloneCountrys = (string[])countrys.Clone();
            Console.WriteLine("-----------------Clone----------------------");
            countrys[1] = "俄罗斯";
            foreach (string ctry in countrys)
            {
                Console.WriteLine(ctry);
            }
            Console.WriteLine("---------------------------------------------");
            foreach (string cctry in cloneCountrys)
            {
                Console.WriteLine(cctry);
            }
        }
    }
}

 

posted on 2012-08-26 22:01  松波  阅读(153)  评论(0编辑  收藏  举报

导航