zoukankan      html  css  js  c++  java
  • 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】

  • 相关阅读:
    电脑连接到手机并安装手机驱动usb-driver
    创建安卓模拟器的两种方式及常用Android命令介绍
    在loadrunner中用头文件的形式对字符串进行MD5加密操作
    Android自动化测试Uiautomator--UiCollection接口简介
    Android自动化测试Uiautomator--UiScrollable接口简介
    Android自动化测试Uiautomator--UiObject接口简介
    Android自动化测试Uiautomator--UiSelector接口简介
    Uiautomator简介及其环境搭建、测试执行
    Android自动化测试Uiautomator--UiDevice接口简介
    Eclipce 配置javaEE
  • 原文地址:https://www.cnblogs.com/ysz12300/p/5283297.html
Copyright © 2011-2022 走看看