您的位置:首页 > 编程语言 > C#

【C#】基础知识—数组

2016-03-02 23:27 489 查看
一、概述

在C#程序设计中,数组与字符串一样,是最常见的类型之一,数组能按一定的规律把相关的数据组织在一起,并能通过“索引”或者“下标”快速地管理这些数据;集合也可以存储多个数据,C#中最常用的集合是ArrayList集合。

1. 数组:可以帮我们一次声明多个同类型的变量。这些变量在内存中是连续存储的。

数组是通过指定数组的元素类型、数组的秩(维数)及数组每个维度的上限和下限来定义的,即一个数组的定义需要包含三种元素:

 1)元素类型

 2)数组的维数

 3)每个维数的上下限

2. 保存多个值,几乎所有任意类型都可以声明数组。

3. 数组的声明:

数据类型[] 数组名 = new 数据类型 [数组长度];
int []a;  // 表示声明一个int类型的数组
int a;    // 表示声明一个int类型的变量
int [] score = new int [5];  // 就是声明了一个数组,里面包含五个int类型的变量。

注意:声明数组的时候可以指定数组的长度,也可以不指定,也可以在使用数组元素前动态指定,但是数组的长度一经指定就不能更改

C#声明:

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

namespace _01第一个项目
{
class Program
{
static void Main(string[] args)
{
#region  数组
int []a = new int[15];
a[0] = 15;
Console.WriteLine(a[0]);
#endregion

Console.Clear(); // 清屏
int []arr = {1, 2, 3};  // 定义的时候直接进行初始化
}
}
}


C语言声明:
void main()
{
int a[5];
a[0] = 15;
printf("%d", a[0]);

printf("你好 \n");
system("pause");
}

二、二维数组的声明和使用

1. 概念及声明

二维数组即维数为2,二维数组类似于矩形网格和非矩形网格。

二维数组的声明:语法

type[,] arrayName;
type[,] :数组存储数据的数据类型

arrayName:数组名称

2. 初始化

二维数组的初始化有两种方式:

 1)可以通过new运算符创建数组,并将数组元素初始化为他们的默认值

int[,] arr = new int[2, 2] { { 1, 2 }, { 2, 3 } };
 2)也可以在初始化数组时,不指定行数和列数,而是使用编译器根据初始值的数量来自动计算数组的行数和列数
int[,] arr2 = new int[,] { { 1, 2 }, { 2, 3 } };
注意:二维数组分为二维矩形数组和二维交错数组两种。交错数组被称为“数组的数组”,在定义时,每个元素的new运算符不能省略。

实际上,初始化数组时可以省略new运算符和数组的长度。

3. 动态数组

动态数组的声明实际上就是将数组的定义部分和初始化部分分别写在不同的语句中,动态数组的初始化也需要使用new关键字new关键字为数组元素分配内存空间,并为数组元素赋初值。

int m = 1;
int n = 2;
int[,] arr3 = new int[m, n];


三、数组的基本操作

1. 数组的遍历

使用foreach可以实现数组的遍历功能

2. 添加/删除数组

注意:由于数组的长度一经指定就不能更改,因此利用索引号对数组元素进行删除并不能真正实现数组元素的删除。

案例:

public static void test18()
{
// string[] str = { "课本", "作业", "铅笔", "橡皮" };
string[] str = new string[]{ "课本", "作业", "铅笔", "橡皮" };
// string strKind = Convert.ToString(Console.ReadLine());  // 定义字符串接收输入的字符串
int length = str.Length;
Console.WriteLine("原数组是:");
foreach (string s in str)
{
Console.Write(" {0}", s);
}
repeat:
// 输入提示的条件
Console.WriteLine("\n 请输入要删除的数组元素的索引(索引大于等于0小于{0}): ", length);
operate:
int index = Convert.ToInt16(Console.ReadLine());   // 获取要删除元素的索引
if (index < 0 || index >= str.Length)
{
Console.WriteLine("输入的数据不合理,请重新输入");
goto operate;
}
else
{
for (int i = index; i < str.Length - 1; i++)
{
str[i] = str[i + 1];
}
Console.WriteLine("删除成功");
length--;
}
Console.WriteLine("新数组为: ");
foreach (string s in str)
{
Console.Write(" {0}", s);
}
if (length != 0)  // 如果新数组中还有元素
{
goto repeat;  // 继续删除操作
}
Console.WriteLine();

}


3. 对数组进行排序

数组案例“冒泡排序”:(其余几种略)

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

namespace _01第一个项目
{
public struct Person
{
public int Age;
public string Name;
public int Height;
public string Hobby;
}
class Program
{
static void Main(string[] args)
{
#region 冒泡排序
int[] score = { 18, 22, 11, 34 };
for (int i = 0; i < score.Length - 1; i++)  // 控制比较的次数
{
for (int j = 0; j < score.Length - 1 - i; j++)  // 逐次比较相邻的两个元素
{
if (score[j] < score[j + 1])
{
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
Console.Write("第{0}趟的排序结果是: ", i);
for (int k = 0; k < score.Length; k++)  // 输出每一次比较的结果
{
Console.Write(score[k] + "\t");
}
Console.WriteLine();
}
Console.ReadKey();
#endregion
}
}
}

4. Array类的Sort和Reverse排序方法

前面的遍历方法比较麻烦,要是对于一些简单的数据的话,实际中往往会运用C#本身提供的数组排序的方法Array.Sort和Array.Reverse方法。

Array.Sort:对以为数组的元素进行排序

Array.Reverse:反转一维数组或部分数组元素的顺序

public static void test19()
{
int []arr = {3, 9, 27, 6, 19};
Array.Sort(arr);
Console.WriteLine("数组排序后是:");
foreach (int i in arr)
{
Console.Write("{0} ", i);
}
// 反向排序
Array.Reverse(arr);
Console.WriteLine();
Console.WriteLine("数组反向排序后是: ");
foreach (int i in arr)
{
Console.Write("{0} ", i);
}
Console.WriteLine();
}
四、ArrayList类

ArrayList类相当于一种高级的动态数组,它是Array类的升级版本。(可理解是可变数组)

ArrayList类位于using System.Collections命名空间下,它可以动态添加和删除数组元素。可以将ArrayList类看做是扩充了功能的数组,但它并不等同于数组。P101课本

主要的方法::增Add、删Remove

public static void test20()
{
ArrayList count = new ArrayList();
count.Add("nihao");
count.Add("hello");
count.Add("welcome");
count.Add("haha");
Console.WriteLine("增加元素之后的数组是: ");
foreach (string s in count)
{
Console.Write("{0} ", s);
}

// 删除
count.RemoveAt(0);  // 删除指定索引的元素
count.RemoveRange(0, 1); // 删除指定范围的元素
count.Remove("welcome");  // 删除与obj匹配的元素
count.Clear();   // 清除所有元素

Console.WriteLine("");
Console.WriteLine("删除元素之后的数组是: ");
foreach (string s in count)
{
Console.Write("{0} ", s);
}

}
区分:C#数组与C++数组

由于C#数组与C++数组在声明上有类似之处,语法上基本相同,所以有时候会误以为这二者没有区别。但是C#中的数组和C++中的数组有一定的区别,C#中的所有数据都是以类型的方式表达,数组也不例外,存储不同类型元素的数组本身也是一种类型,C#中所有的数组类型都继承自Aystem.Array,该类型直接继承自System.Object并实现多个了接口,由于所有直接或者间接继承自System.ValueType的类型才属于值类型所以C#中的数组是引用类型而C++中的数组是值类型,两者在存储数据时是有区别的。

五、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息