C# 数组

 

1,建立数组

代码:

int[]i = new int[12];      // 声明并定义
int[] ii;                  //先声明
ii = new int[12];          //后初始化
ii[2] = 12;                //赋值
Console.WriteLine(ii[2]);  //取出

2,函数中数组的传递

代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             
            double []d = new double[12];
            d[0] = 11.11;
            d[1] = 11.11;
            d[2] = 11.11;
            ModifyArray(d);                  //传引用,函数内可修改,但只能修改成员,不能修改引用本身
              Console.WriteLine(d[0]);
            ModifyOne(d[1]);                 //传值,函数内不可修改,只是副本而已
              Console.WriteLine(d[1]);
            ModifyTwo(ref d[2]);             //传引用,函数内可修改
              Console.WriteLine(d[2]);
            ModifyThree(ref d);
            Console.WriteLine(d[1]);         //传引用引用本身,不但可以修改成员,也可修改引用本身
         }
        public static void ModifyArray(double[] b) //注意声明函数时用 public static可以解决不建立实例而直接调用
        {
            b[0] = 22.22;
            b = new double[] { 1, 2, 3, 4 }; //修改引用本身是无效的
          }
        public static void ModifyOne(double b)
        {
            b = 22.22;
        }
        public static void ModifyTwo(ref double b)
        {
            b = 22.22;                      //单个成员传引用可以采用
         }
        public static void ModifyThree(ref double[] b)
        {
            b = new double[]{1,2,3,4};      //修改引用本身,指向一个新建立的数组
         }
    }
}

结果:

tmpEA

3,多维数组

代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           int[,] a = {{1,2},{3,4}}; //矩形数组
            Console.WriteLine("{0},{1},{2},{3}", a[0, 0], a[0, 1], a[1, 0], a[1, 1]);//注意与C++写法不一样
            int[][] array = {
                                new int[] {1,2},
                                new int[] {3},
                                new int[] {4,5,6}
                            };                           //齿状数组
              int[,] b;
            b = new int [3,4]; //3行4列
              int[][] c; //齿状数组,注意与矩形数组的区别 int[,]
            c = new int[2][];
            c[0] = new int[5];
            c[1] = new int[3];

            Console.WriteLine("Print Array a:");
            DisplayArray(a);
            Console.WriteLine("Print Array array:");
            DisplayArray(array);
        }
        public static void DisplayArray(int [,] array)  //矩形数组的遍历
         {
            for (int row = 0; row < array.GetLength(0); row++)  //GetLength()参数0代表第一维长度,一词1代表第二维长度,还有array.Length属性表示所有数组成员个数
            {                                                   //对于1维数组来讲,array.GetLength(0)和array.Length一回事
                for(int column = 0;column< array.GetLength(1);column++)
                {
                    Console.Write("{0} ",array[row,column]);
                }
                Console.WriteLine();
            }
            foreach (int data in array) //foreach关键字,遍历所有元素的简单写法,锯齿数组不能这样写
              {
                Console.Write("{0} ", data);
            }
            Console.WriteLine();
        }
        public static void DisplayArray(int [][] array) //齿状数组的遍历
        {
            for (int row = 0; row < array.Length; row++)
            {
                for (int column = 0; column < array[row].Length; column++)
                {
                    Console.Write("{0} ", array[row][column]);
                }
                Console.WriteLine();
            }
        }
    }
}

结果:

tmp56

4,变长参数列表

把多个参数项,当成一个列表传递到函数体中

代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i1 = 1;
            int i2 = 2;
            int i3 = 3;
            int i4 = 4;
            DisplayAllData(i1);
            DisplayAllData(i1,i2);
            DisplayAllData(i1,i2,i3,i4);
        }
        public static void DisplayAllData(params int[] number) //用params,表示可以接受多个参数
        {
            foreach (int i in number)
            {
                Console.Write("{0} ",i);
            }
            Console.WriteLine();
        }
    }
}

结果:

 image

【END】

原文地址:https://www.cnblogs.com/ysz12300/p/5283297.html