数组是相同数据类型的元素按一定顺序排列的集合,然后用一个变量名进行命名。新建控制台程序,声明一个静态方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DemoArray_1
{
class Program
{
static void Main(string[] args)
{
ArraysDemo();
Console.ReadLine();
}
static void ArraysDemo()
{
//定义数组变量
int[] myInts = new int[4];
string[] booksOnDotNet = new string[100];
//使用索引来填充数组
myInts[0] = 110;
myInts[1] = 120;
myInts[2] = 114;
myInts[3] = 119; foreach (int i in myInts)
Console.WriteLine(i);
Console.WriteLine();
}
}
}

上面代码是使用构造函数来声明数组的,在声明的时候,并没有显式的填充数组值,此时数组中的每一项都会被设置成数据类型的默认值,比如Int类型的数组项默认值会被设置成0,bool类型数组则被设置成false

数组初始化

static void ArrayInit()
{
//使用new关键字来创建数组,未指定大小
string[] ColorsArray = new string[] { "red", "green", "blue", "black" };
Console.WriteLine("ColorsArray has {0} elemnets", ColorsArray.Length);
//不使用new关键字,使用字面值初始化数组
int[] VarArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("VarArray has {0} elements ", VarArray.Length);
//使用new关键并指定大小,创建数组
char[] CharArray = new char[3] { 'a', 'b', 'c' };
Console.WriteLine("CharArray has {0}", CharArray.Length); }

在用花括号创建数组时,不需要指定数组的大小,由花括号中的元素个数自动确定数组大小,另外new关键字也是可选的

定义隐式类型本地数组

static void DefinedImplicitArray()
{
//使用var关键字来定义隐式类型本地数组,注意数组元素必须是同一类型
var myInt=new[] {110,120,114,119};
Console.WriteLine("myInt is {0}", myInt.ToString()); }
需要注意是数组元素的类型必须是同一类型
定义Object数组
我们知道在.Net中所有内置类型的父类型都是System.Object类型,如果定义一个Object类型的数组,会发生什么?

static void ObjcetsArray()
{
object[] myObjects = new object[3];
myObjects[0] = 1; //int类型
myObjects[1] = "this is a demo"; //string类型
myObjects[2] = true; //boolean类型
foreach (object i in myObjects)
Console.WriteLine("Type is {0},Value is {1}", i.GetType(), i);
}

输出如下:

从输出可以看出,使用object创建数组时,元素可以是任何类型,因为所有内置类型最终都会被转化成Object类型

定义矩形数组

除一维数组外,C#还支持两种多维数组:①矩形数组;②交错数组

矩形数组:每行长度相同,如下面的二维数组

static void RectArray()
{
int[,] MatrixArray;
MatrixArray=new int[10,10];
//填充数组
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
MatrixArray[i, j] = i + j; //输出数组
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
Console.Write(MatrixArray[i, j] + "\t");
Console.WriteLine(); }

在上面的代码中使用"\t" 用于插入一个水平制表符,并且使用Write输出,以获得6x6的矩形输出效果
定义交错数组
交错数组也就是数组中数组

 static void CrossArray()
{
//声明一个具有3个不同数组的数组
int[][] myCrossArray = new int[3][];
Console.WriteLine(myCrossArray.Length); //获取的数组大小是3,即上面代码是声明三个数组
//创建交错的数组
for (int i = 0; i < myCrossArray.Length; i++)
myCrossArray[i] = new int[i + 7]; //第一个数组元素有7个,第二个数组元素8,依次类推
//输出数组
for(int i=0;i<3;i++)
{
for(int j=0;j<myCrossArray[i].Length;j++)
Console.Write(myCrossArray[i][j]+"\t");
Console.WriteLine();
}
}

上面代码没有给交错数组 填充值,所以所有数组元素的默认值都为0,下图是上面两种数组运行结果

用数组传参

 //向方法中传入数组
static void PrintArray(int[] ints)
{
for (int i = 0; i < ints.Length; i++)
Console.WriteLine("{0}的索引是{1}", ints[i], i);
}
//从方法获取string类型的数组
static string[] GetValueArray()
{
string[] varString = { "one", "two", "three" };
return varString;
}
在Main()方法中,进行调用
//调用PrintArray方法
int[] tels = { 110, 120, 114, 119 };
PrintArray(tels);
//调用GetValueArray,会获得一个数组
string[] strs = GetValueArray();
foreach (string s in strs)
Console.WriteLine(s);

System.Array功能演示

每一个被创建的数组都继承自Array基类。Array基类有很多常用功能,特别是静态成员,可以在类级别进行调用。

下表是Array的常用成员

 static void SystemArray()
{
string[] strs = { "China" };
//利用Length属性,输出数组元素
for(int i=0;i<strs.Length;i++)
{
Console.Write(strs[i] + "\t");
}
//利用Reverse()方法反转数组
Array.Reverse(strs);
for (int i = 0; i < strs.Length; i++)
{
Console.Write(strs[i] + "\t");
} //待续
}

C#_数组的更多相关文章

  1. 选择排序_C语言_数组

    选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...

  2. 插入排序_C语言_数组

    插入排序_C语言_数组 #include <stdio.h> void insertSort(int *); int main(int argc, const char * argv[]) ...

  3. 快速排序_C语言_数组

    快速排序_C语言_数组 #include <stdio.h> void quickSort(int *, int, int); int searchPos(int *, int, int) ...

  4. 冒泡排序_C语言_数组

    冒泡排序_C语言_数组 #include <stdio.h> //冒泡排序 小->大 void sort(int * pArray, int len); int main(int a ...

  5. Net基础篇_学习笔记_第九天_数组_冒泡排序(面试常见题目)

    冒泡排序: 将一个数组中的元素按照从大到小或从小到大的顺序进行排列. for循环的嵌套---专项课题 int[] nums={9,8,7,6,5,4,3,2,1,0}; 0 1 2 3 4 5 6 7 ...

  6. Net基础篇_学习笔记_第九天_数组_三个练习

    练习一: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  7. Net基础篇_学习笔记_第九天_数组

    结构:一次性存储不同类型的变量: 数组:一次性存储相同类型的变量: 数组的语法: 数组类型[ ] 数组名=new 数组类型[数组长度]: int[ ] nums=new int[10]; 数组初值都是 ...

  8. C++_系列自学课程_第_7_课_数组_《C++ Primer 第四版》

    说到数组,大家应该都很熟悉,在C.Pascal.Java等语言中,都有数组的概念.在C++中也提供了对数组的支持.数组简单来说就是一堆相同 数据类型对象的集合. 这里要把握住两个要点: 相同的数据类型 ...

  9. 大数计算_BigNum优化_加减乘除乘方取余_带注释_数组

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...

  10. Excel 信息对比_数组版

    Sub LOOKUP_UChur() Dim i As Long '=== sourceWorksheet = 数据源表名称 Dim sourceWorksheet As Worksheet Dim ...

随机推荐

  1. 用DataBaseMail发图片并茂的邮件

    不知道各位的老板有没有这样的要求, 一些系统中的数据需要定时发出邮件提醒, 如呆料就要到期或者一些待办的事项提醒. 当然这些用SSRS报表订阅可以实现,但有些公司没有设定相应的报表服务,又或者只是一些 ...

  2. javascrip keyCode属性备案

    keycode    8 = BackSpace BackSpacekeycode    9 = Tab Tabkeycode   12 = Clearkeycode   13 = Enterkeyc ...

  3. LVS测试小结

    RedHat5实现负载均衡 http://blog.sina.com.cn/s/blog_4e424e2101007rie.html http://www.doc88.com/p-9975618478 ...

  4. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  5. HDU 4282 A very hard mathematic problem 二分

    A very hard mathematic problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sh ...

  6. uva 387 A Puzzling Problem (回溯)

     A Puzzling Problem  The goal of this problem is to write a program which will take from 1 to 5 puzz ...

  7. 移动端折腾国外分享(facebook、twitter、linkedin)

    一.前言 国内做HTML5页面,关注最多就是微信分享了,之前也写过关于微信分享的文章,可以点击查看:分享相关文章 再者,就是国内的其它分享,比如常用的新浪微博.腾讯微博.QQ空间等等,最方便的就是直接 ...

  8. 深入理解JavaScript 模块模式

    http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html 模块模式是JavaScript一种常用的编码模式.这是一般的 ...

  9. 【Python】 做一个简单的 http server

    # coding=utf-8 ''' Created on 2014年6月15日 @author: Yang ''' import socket import datetime # 初始化socket ...

  10. MySQL 调优基础:Linux内存管理 Linux文件系统 Linux 磁盘IO Linux网络

    http://www.cnblogs.com/digdeep/category/739915.html