c#中的数据类型简介(数组)

c#中的数据类型简介(数组)

数组定义

可以将数组看成相同数据类型的一组或多组数据,包括一维数组,多维数组和交错数组

数据申明

一维数组的几种申明和实例化

type[]  typeName = new type[n];                                                //定义数组但是未赋值

type[0] = item1;

type[1] = item2;

type[2] = item3; ......

type[n-1] =itemn;

type[]  typeName = new type[n] { item1,item2,item3,......itemn};  //系统可以自动推算数组长度

type[]  typeName = new type[] { item1,item2,item3,......itemn};  //系统可以自动推算数组长度

type[]  typeNmae = {item1,item2,item3,......itemn};                   //省略赋值的数据类型,系统可以自动推断,非常简洁的定义方式

    class Program
    {
        static void Main(string[] args)
        {
            int[] intA = new int[5];
            intA[0] = 1;
            intA[1] = 2;
            intA[2] = 3;
            intA[3] = 4;
            intA[4] = 5;
            int[] intB = new int[] { 1, 2, 3, 4, 5 };
            int[] intC = new int[5] { 1, 2, 3, 4, 5 };
            int[] intD = { 1, 2, 3, 4, 5 };

            //数组的取值,返回索引位3的值,返回的值为3
            int item3 = intA[2];
            //int[]的抽象基类System.Array,继承了IEnumerable接口,可以使用foreach
            foreach (int item in intA)
                Console.WriteLine("item is:{0}", item);
            //可以使用for循环访问数组成员
            for (int i = 0; i < intA.Length; i++)
                Console.WriteLine("each is :{0}", intA[i]);
            //表明数组成员可以被赋值
            bool b1 = intA.IsReadOnly;     //返回false
            bool b2 = intA.IsSynchronized; //返回false,可以异步访问
            //数组一经定义好后,是不能被新增、插入、删除的
            bool b3 = intA.IsFixedSize;    //返回true,表示数组是固定长度的

            //多维数组申明
            string[,] strA = new string[3,2]{ { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strB = new string[,] { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strC = { { "GZ", "SZ" }, { "CD", "DZ" }, { "CS", "ZZ" } };
            string[,] strD = new string[3, 2];
            strD[0, 0] = "GZ"; strD[0, 1] = "SZ";
            //多维数组的取值
            string item11 = strA[1, 1];
            string itemX = strD[2,1]; //初始化未赋值为null
            //多维数组成员foreach遍历
            foreach (string item in strA)
                Console.WriteLine("foreach遍历:{0}",item);
            //多维度成员的for遍历
            int w1 = strA.GetUpperBound(1)+1;
            int w2 = strA.GetUpperBound(0)+1;
            for (int i = 0; i < w2; i++)
                for (int j = 0; j < w1; j++)
                    Console.WriteLine("for遍历:{0}",strA[i, j]);

            //交错数组,表示成员为数组的数组
            int[][] arry = new int[2][];
            arry[0] = new int[3] { 6, 7, 8 };
            arry[1] = new int[4] { 9, 4, 5, 6 };
            foreach (var item in arry)
                foreach (int element in item)
                    Console.WriteLine(element);
        }
    }

数组抽象基类System.Array

System.Array是具体数组的抽象类,具体数组继承自System.Array类

        static void Main(string[] args)
        {
            //使用System.Array静态方法CreateInstance创建数组实例
            Array strArray = Array.CreateInstance(typeof(string), 4);
            strArray.SetValue("beijing", 0);
            strArray.SetValue("shanghai", 1);
            strArray.SetValue("tianjin", 2);
            strArray.SetValue("chongqin", 3);
            //上面的写法等价于
            Array strArray1 = new string[4]{"beijing","shanghai","tianjin","chongqin"};
            //foreach 遍历数组
            foreach (var item in strArray)
                Console.WriteLine("foreach一维数组遍历list:{0}",item);
            //for 遍历数组
            for (int i = 0; i < strArray.Length; i++)
                Console.WriteLine("for一维数组遍历list:{0}",strArray.GetValue(i));
            //反转一维数组strArray
            Array.Reverse(strArray);
            //Array 创建多维数组
            Array strArray2 = Array.CreateInstance(typeof(int), new int[] { 3, 2 });
            strArray2.SetValue(10,new int[]{0,0});
            strArray2.SetValue(11, new int[] { 0, 1 });
            strArray2.SetValue(12, new int[] { 1, 0 });
            strArray2.SetValue(13, new int[] { 1, 1 });
            strArray2.SetValue(14, new int[] { 2, 0 });
            strArray2.SetValue(15, new int[] { 2, 1 });
            //上面的多维数组定义等价于
            int[,] strArray3 = new int[3,2] { { 10, 11 }, { 12, 13 }, { 14, 15 } };
            foreach (var item in strArray2)
                Console.WriteLine("foreach多维数组遍历:{0}",item);
           //0维度的长度为3,其上限为2,其下限为0,运行结果显示OK,OK
            if (strArray2.GetLength(0) == strArray2.GetUpperBound(0) - strArray2.GetLowerBound(0) + 1)
                Console.WriteLine("OK,OK");
        }
时间: 2024-08-14 02:05:52

c#中的数据类型简介(数组)的相关文章

c#中的数据类型简介(枚举)

C#中的数据类型简介(枚举) 枚举的定义 根据MSDN上给出的定义,枚举是一个指定的常数集,其基础类型可以是除Char外的任何整型. 如果没有显式声明基础类型,则使用 Int32. Enum 是 .NET Framework 中所有枚举的基类.其基础类型可以是byte.sbyte.short.ushort.int.unit.long.ulong.默认情况下,第一个枚举数的值为0,然后后续的枚举数依次加1. 枚举的申明 枚举可以申明在命名空间下和类同级别,也可申明在类的内部.申明语法如下: [ac

Oracle中PL/SQL简介、基本语法以及数据类型

Oracle中PL/SQL简介.基本语法以及数据类型 一.PL/SQL简介. Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询和Oracle自身过程控制为一体的强大语言,PL/SQL不但支持更多的数据类型,拥有自身的变量申明,赋值语句,而且还有条件,循环等流程控制语句.过程控制结构与SQL数据处理能力无缝的结合形成了强大的编程语言,可以创建过程和函数以及程序包. PL/SQL是一种块结构的语言,它将一组语句放在一个块中,一次性的发送给服务器,由服

java中基本数据类型数据转化成byte[]数组存储

java中基本数据类型数据转化成byte[]数组存储 1 package com.wocqz.test; 2 3 public class testByte { 4 5 /** 6 * int 转成byte数组 7 * */ 8 public static byte[] int_byte(int id){ 9 //int是32位 4个字节 创建length为4的byte数组 10 byte[] arr=new byte[4]; 11 12 arr[0]=(byte)((id>>0*8)&

C语言基本数据类型简介

C语言基本数据类型简介 1.概述 C 语言包含的数据类型如下图所示: 2.各种数据类型介绍 2.1整型 整形包括短整型.整形和长整形. 2.1.1短整形 short a=1; 2.1.2整形 一般占4个字节(32位),最高位代表符号,0表示正数,1表示负数,取值范围是-2147483648~2147483647,在内存中的存储顺序是地位在前.高位在后,例如0x12345678在内存中的存储如下: 地址:0x0012ff78 0x0012ff79 0x0012ff7a 0x0012ff7b 数据:

redis中各种数据类型对应的jedis操作命令

一.常用数据类型简介: redis常用五种数据类型:string,hash,list,set,zset(sorted set). 1.String类型 String是最简单的类型,一个key对应一个value String类型的数据最大1G. String类型的值可以被视作integer,从而可以让“INCR”命令族操作(incrby.decr.decrby),这种情况下,该integer的值限制在64位有符号数. 在list.set和zset中包含的独立的元素类型都是Redis String类

Python中的数据类型

Python中的数据类型 目录1.字符串2.布尔类型3.整数4.浮点数5.数字6.列表7.元组8.字典9.日期 1.字符串(http://www.cnblogs.com/yjd_hycf_space/p/6846284.html)1.1.如何在Python中使用字符串a.使用单引号(')用单引号括起来表示字符串,例如:str='this is string';print str; b.使用双引号(")双引号中的字符串与单引号中的字符串用法完全相同,例如:str="this is str

C语言中的函数、数组与指针

1.函数:当程序很小的时候,我们可以使用一个main函数就能搞定,但当程序变大的时候,就超出了人的大脑承受范围,逻辑不清了,这时候就需要把一个大程序分成许多小的模块来组织,于是就出现了函数概念:  函数是C语言代码的基本组成部分,它是一个小的模块,整个程序由很多个功能独立的模块(函数)组成.这就是程序设计的基本分化方法: (1) 写一个函数的关键: 函数定义:函数的定义是这个函数的实现,函数定义中包含了函数体,函数体中的代码段决定了这个函数的功能: 函数声明:函数声明也称函数原型声明,函数的原型

如何判断js中的数据类型?

js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: 什么整数啊浮点数啊都叫数字,你懂的~ Boolean:  就是true和false啦 undefined: 未定义,就是你创建一个变量后却没给它赋值~ null:  故名思久,null就是没有,什么也不表示 object: 这个我也很难解释的说.就是除了上面五种之外的类型 如何判断js中的数据类型:type

java中的数据类型和运算符的总结归类。

首先学习java肯定先要了解java的发展史,以及java的特点,常见的dos命令,jdk的安装,如何开发java程序等等一下概念行的东西,这里面我都不一一说了. 今天这一章主要想总结一下java中的数据类型和运算符2大方面. 再说数据类型之前先说一下标识符的命名规则: 总的命名规则:见名知意.如果有多个单词组成,首单词小写,其余单词的首字母大写(驼峰命名法).1.首字母只能是字母,下划线和$2.其余字母可以字母,下划线,$和数字3.不能使用预留关键字4.严格区分大小写(总体来说和c语音一样)