数组基础、多维数组、静态Array

数组:一组数据。本身是一种类型(引用类型),但它中存储的元素(项)也有数据类型,数组只能用来存储类型相同的强类型的数据,比如班级只能用来存放学生,不能放别的。数组在内存中是按定长连续来存储的,具有相同数据类型的数据元素的有序集合。

 int a = 10;// 一个数据
 bool b = true; // 一个数据

 // int[] 数据类型+[] , 3数组长度,可以明确指定,也可以不指定
 // 没有初始化时,指定长度,有初始化时,长度省略。
 int[] ints = new int[3];
 // 通过索引初始化,索引从0开始,到数组长度-1结束,索引不能越界
 ints[0] = 11;
 ints[1] = 12;
 ints[2] = 13;
 //ints[3] = 14;  // System.IndexOutOfRangeException:“索引超出了数组界限。”

 

 数组一旦创建,数组的长度就会固定。

 string[] strings = new string[] { "abc", "hello", "world" };
 Console.WriteLine(strings.Length);  // 数组一旦创建,长度固定。
 //strings.Length = 4;// Length长度是只读的不能修改

 string[] strings2 = { "abc", "hello", "world" };
 int[] ints2 = { 10, 20, 20 }; // 数组中项不是唯一。

 // C#数组支持数组中存储不同数据类型的元素。如:object[]
 object[] objects = new object[] { 1, 2, 'a', "hello", false, new object() { } };

一维数组,多维数组(2维,3维)

创建数组实例后,其每个元素都有默认值,string 的为空字符串,int 的为 0 ,bool 的为 false,引用类型的为 null;判断数组是几维,就看[]中逗号,0个逗号是一维数组,1个就是二维,依次类推。

一维数组:

// 一维数组
Student[] students2 = {
    new Student(){ Id=1,Name="张三1"},
    new Student(){ Id=2,Name="张三2"},
    new Student(){ Id=3,Name="张三3"},
};

二维数组:不是ArrayList,ArrayList没有多维情况,多维数组指的是Array 静态定义方式类型[,] 名字 = new 类型[行数,列数]

// 二维数组
Student[,] stuTable = new Student[,] {
    {
        new Student(){ Id=11,Name="张三1"},
        new Student(){ Id=12,Name="张三2"},
        new Student(){ Id=13,Name="张三3"},
    },
    {
        new Student(){ Id=21,Name="李四1"},
        new Student(){ Id=22,Name="李四2"},
        new Student(){ Id=23,Name="李四3"},
    },
    {
        new Student(){ Id=31,Name="王五1"},
        new Student(){ Id=32,Name="王五2"},
        new Student(){ Id=33,Name="王五3"},
    }
};

对数组的基本操作(重要):静态Array,动态

static void Add(int x)
{
    // fun===Addd, ()===(int x), { }方法体   =>箭头函数(JS), 拉姆达表达式(理解匿名方法)
    Action fun = () => { };
}

static void Method1(int item)
{
    Console.WriteLine(item);
}
 
// 隐式的初始化(隐式类型化数组)
 var arr = new int[] { 1, 2, 3 };
 // var==int[],编译期间确定数组类型,dynamic运行期间确定类型

 // 追加
 arr = arr.Append(4).ToArray();
 Console.WriteLine(arr[3]);  // 4

 // 循环  for,while,foreach
 for (int i = 0; i < arr.Length; i++)
 {
     Console.WriteLine(arr[i]);
 }

 // 委托:看见委托,传一个方法过去。
 // 参数1:循环的数组 参数2:拉姆达表达式(回调函数)    ForEach数组专门API。
 Array.ForEach(arr, (item) =>
 {
     Console.WriteLine(item);
 });

// Method1回调函数
Array.ForEach(arr, Method1);  // 不能添加小括号

// 删除(和其他对象的删除不太一样,只能修改,数组一旦创建,长度固定)
Array.Clear(arr, 0, 4);  // 把项的值改成默认值
Array.ForEach(arr, Method1);

// 修改(替换某个项,整个数组修改)
arr[0] = 10;  // 通过索引修改,注意影响原数组
Array.ForEach(arr, Method1);

arr = new int[] { 100, 200, 300, 400 }; // 修改地址
Array.ForEach(arr, Method1);
// 插入(C#数组不支持)

// 查询
var filterArr = arr.Where((item) =>
{
    return item > 100;
}).ToArray();

Array.ForEach(filterArr, Method1);

int result = Array.Find(arr, (item) =>
 {
     return item == 200;
 });
Console.WriteLine(result);

Student oneStudent = Array.Find(students2, (item) =>
{
    // item===一个学生对象
    return item.Name.EndsWith("2");
});
Console.WriteLine($"学生Id:{oneStudent.Id},学生姓名:{oneStudent.Name}");

Student[] stus = Array.FindAll(students2, (item) =>
{
    // item===一个学生对象
    return item.Name.EndsWith("张");
});

Array.ForEach(stus, (stu) =>
{
    Console.WriteLine(stu.Name);
});

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值