C#基础3 数组与数组的基本结构

https://blog.csdn.net/NCZ9_/article/details/83651014

数组:一种数据结构,它可以包含同一类型的多个元素。

数组元素:数组中的数据。

访问数组元素:数组名[下标]  ——————> 下标从0开始。

数组长度(Length):数组中的元素个数。

遍历:就是有序地查看数组的每一个元素。

一维数组:数据类型[] 数组名;(示例:int[]a;)

格式:数组名=new 数据类型[数组元素个数]{数组元素列表};

int[]A=new int[3]{0,1,2};

简化后:数据类型[]数组名={数组元素列表};

int[]A={0,1,2,3};

遍历

正常
for (int i = 0; i < A.Length; i++)
                {
                    Console.WriteLine(A[i]);
                }
简化
foreach (var item in A)
               {
                   Console.WriteLine(item);
               }

二维数组:数据类型[,]数组名(示例:int[,]a)

格式:数组名=new 数据类型[数组个数][数组元素个数];

正常
int[,]b=new int[2,3]{{1,2,3},{0,4,5}};
简化
int[,] b = { {1,5,6 },{7,5,3 } };

遍历

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
{
    Console.WriteLine("{0}行{1}列元素为{2}", i, j, b[i, j]);
}
}
简化后
foreach (var item in b)
    {
        Console.WriteLine(item);
    }
        Console.ReadKey();

多维数组:数据类型[,,] 数组名;(示例:int[,,]c;)

数组名 = new 数据类型[页数,行数,列数];

示例:c = new int[2,3,5];
完整:int[,,] B = new int[2,3,2]{{{1,2}, {3, 4 }, {5,6} }, {{1,2}, {3, 4}, {5, 6}}};

写多维数组时注意,的位置是否正确,是否缺少。

多维数组就是在创建多个上一维数组,并将其整合。

交错/锯齿数组:数据类型[][]数组名;(示例:int[][]d;)

交错数组的每个元素又是一个数组

数据类型[][]数组名;(示例:int[][]d;)第一个[]中代表有多少个数组,第二个中代表多维数组(写法和多维数组相同)

int[][,] d = new int[3][,] {new int[2,1]{{4},{6}},new int[2,2]{{1,2},{3,4}},new int[2,2]{ {5,4}, {4,3}}};

遍历

foreach (var item in d)
{

     foreach (var items in item)

  {

      Console.WriteLine(items);

  }

}

数组的基本操作

Enumerable类

.Contains

判断数组是否包含制定数值
int[] arr = new int[3] {5,6,7};
Console.WriteLine(arr.Contains(8)); 
--->
False

.

Sum/Max/Min/Average

求和/最大值/最小值/平均数

int[] arr = new int[3] {5,6,7};
Console.WriteLine(arr.Sum());

Console.WriteLine(arr.Max());

Console.WriteLine(arr.Min());

Console.WriteLine(arr.Average());
--->
18
7
5
6

Count

返回序列中的元素数,只能在一维数组中使用

int[] arr = new int[3];

Console.WriteLine(arr.Count());
--->

3

Array类

Rank

获取数组的稚数,(几维数组返回多少,一维数组返回1,二维数组返回2)

int[,] arr = new int[2, 3] { { 14, 25, 9 }, { 5, 74, 56 } };
Console.WriteLine(arr.Rank); ---> 2

Length

获取所有维度中元素数的总数
int[] arr = new int[5] { 1, 5, 6, 8, 7 };
Console.WriteLine(arr.Length); ---> 5

clear

清除指定位置中的元素:下例中从下标1既"ab"开始清除3个,然后输出
string[] arr = new string[5] { "a", "ab", "abc", "abcd","abcde" };

Array.Clear(arr,1,3);

foreach (var item in arr)
{
     Console.WriteLine(item);
}
--->

a
  
abcde

//参数说明:

//arr:需要清除其元素的数组

//1:要清除的一系列元素的起始索引

//3:要清除的元素数

ConstrainedCopy将某数组中指定范围元素复制到另一数组指定位置。

Z为被复制数组,X为复制数组,下例中从Z的下标为2的"zxc"开始复制
复制到X中下标为1,既w的后面三位
string[] Z = new string[5] { "qwe", "asd", "zxc", "rty", "fgh" };
string[] X = new string[4] { "q", "w", "e", "r" };
Array.ConstrainedCopy(Z,2,X,1,3);
所以当输出X时,
foreach (var item in X)
    {
       Console.WriteLine(item);
    }
————————————
q
asd
zxc
rty

copy将某数组中指定范围元素复制到另一数组。

复制arr1中元素到arr2中   复制两个
string[] arr1 = new string[3] { "aa", "bb", "cc"};

string[] arr2 = new string[3];

Array.Copy(arr1, arr2,2);
foreach (var item in arr2)
{
   Console.WriteLine(item);
}

--->
aa
bb
//参数说明:
//arr1:要复制的数据的数组
//arr2:接收数据的数组
//2:要复制的元素数目

Index of 在一个一维数组中搜索指定对象,并返回其首个匹配项的索引。

查找元素,返回下标。下标从0开始

string[] arr1 = new string[4] { "aa", "bb", "bb" , "cc" };
Console.WriteLine(Array.LastIndexOf(arr1, "bb")); ---> 2

resize 更改数组长度,将一维数组中元素数更改为指定大小

数组中元素数为四个 通过.Resize更改添加"ee"   并将arr[4]添加为"ee"
string[] arr = new string[4] { "aa", "bb", "cc" , "dd" };

Array.Resize(ref arr,5);

arr[4] = "ee";
foreach (var item in arr)
{
    Console.WriteLine(item);
}

--->
aa
bb
cc
dd
ee

sort为一维数组元素排序


int[] arr = new int[5] { 5, 3, 4 , 1 ,2};
Array.Sort(arr);
foreach (var item in arr)

{
    Console.WriteLine(item);
}

--->
1
2
3
4
5

Reverse元素反转,交换位置

int[] P = { 1, 5, 6, 4 };
Array.Reverse(P, 0, 3);        从数组P的下标为0开始后面3位
foreach (var item in P)
   {
     Console.WriteLine(item);
   }

SetValue更改数组指定下标的元素。


int[,] arr = new int[2, 3] { {2,3,4 },{5,6,7 } };
arr.SetValue(8,1,1);             将8更改至坐标1,1处
Console.WriteLine(arr[1,1]);
--->
8

猜你喜欢

转载自blog.csdn.net/weixin_43962466/article/details/88179479