数组 及其部分API

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

     1. 数组就是一组数据,本身是一种类型(引用类型),储存的元素(项)有数据类型
      int x = 0; //属于一个数据
      bool y = false;//也是一个数据
      //int[] 数组名= new 数据类型[数组长度]{数据};
      int[] innts = new int[3];//3:指的就是数组长度,数组长度可以明确指定,也可不指定
                               //数组长度初始化了,所填数据可省略(根据需要填写)
      innts[0] = 23;
      innts[1] = 44;
      innts[2] = 55;//可以通过索引初始化数组,索引是从0开始,到数组长度-1结束,索引不能越界
      int[] int5 = { 1, 2, 3, 4, 5 };
      2.数组长度没有初始化,数据长度必填。数组一但创建,长度固定

 string[] str = new string[] { "asd","zxc","hhh"};
      Console.WriteLine(str.Length);//数组长度
//输出结果为:
//3

      3.数组中支持存储不同类型的元素。如:object[]
      object[] objects = new object[] { 1, 2, 3, "aaa", false, new object() { } };

      4.数组分为:一维数组,多为数组(二维数组,三维...) 
      创建数组实例后,其每个元素都有默认值,
      string--为空字符串,int--为0,bool--为 false,引用类型--为 null;*/

      Student[] student = new Student[]
      {
           new Student(){ Id=1,Name="张三1"},
          new Student(){ Id=2,Name="张三2"},
          new Student(){ Id=3,Name="张三3"},
      };

判断数组是几维,就看[]中逗号,0个逗号是一维数组,1个就是二维,依次类推。
   

  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"},  
          }
      };
      Student student1 = stuTable[2, 1];
      Console.WriteLine("取第3行第2个:" + student1.Name);
//输出结果为:
//取第3行第2个:王五2

      5.隐式的初始化
      var arr = new int[] {1,2,3,4,5};

      6.对数组的基本操作:静态Array  动态
      //追加

var arr = new int[] {1,2,3,4,5};
      arr = arr.Append(4).ToArray();
      Console.WriteLine(arr[4]);
//输出结果为:
//1 2 3 4 5 4


      //循环  for  while foreach
     

  var arr = new int[] {1,2,3,4,5};
for (int i = 0; i < arr.Length; i++)
      {
          Console.WriteLine(arr[i]);
      }
//输出结果为:
//1 2 3 4 5 6
      foreach (var item in arr)
      {
          //item是数组中的某项值,不是索引
          Console.WriteLine(item);
      }
//输出结果为:
//1 2 3 4 5 6

  //ForEach数组独有的API 参数1:循环的数组  参数2:拉姆达表达式(回调函数)

  Student[] students2 = {
     new Student(){ Id=1,Name="张三1"},
     new Student(){ Id=2,Name="张三2"},
     new Student(){ Id=3,Name="张三3"},
 };
  Student[] student2 = Array.FindAll(student, (item) =>
 {
     return item.Name.StartsWith("张");
 });
 for (int i = 0; i < student2.Length; i++)
 {
     Console.WriteLine(student2[i].Name);
 }
      Array.ForEach(student2, (stu) =>
      {
          Console.WriteLine(stu.Name);
      });
static void Method1(int item)
  {
      Console.WriteLine(item);
  }

      //拉姆达表达式(回调函数)
      Array.ForEach<int>(arr,(item) =>
      {
          Console.WriteLine(item);
      });
     
      //Method1 回调函数(item)
      Array.ForEach(arr, Method1);//不能加小括号,加了就变成了方法调用


   //删除  把项的值改成默认值
      Array.Clear(arr,0,4);
      Array.ForEach(arr, Method1);

      //修改

      var arr = new int[] {1,2,3,4,5};
      arr[0] = 10;//通过索引修改,直接影响元素组值
      arr = new int[] {100,200,300,400};//修改了地址

      //插入(c#数组不支持插入)

      //查询

static void Method1(int item)
  {
      Console.WriteLine(item);
  }

  static void Main(string[] args)
  {
var arr = new int[] {100,200,300,400};
   var filArr = arr.Where((item) =>
      {

          return item >100;
      }).ToArray();
      Array.ForEach(filArr, Method1);

}
//输出结果为:
//200 300 400

      //Find 查找
    例子:

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

     (2) Student onestudent = Array.Find(student, (item) =>
      {
          return item.Name.EndsWith("1");
      });
      Console.WriteLine($"学生Id:{onestudent.Id},学生姓名:{onestudent.Name}");
    //FindAll
      Student[] student2 = Array.FindAll(student, (item) =>
      {
          return item.Name.StartsWith("张");
      });
      for (int i = 0; i < student2.Length; i++)
      {
          Console.WriteLine(student2[i].Name);
      }
   

//Exists   判断数组中是否存在满足条件的项,存在返回true,否则返回false。

 string[] strings = { "a", "abc", "abcd", "ba" };
 bool result1 = Array.Exists<string>(strings, (item) =>
 {
     bool r = item.StartsWith("a");
     return r;
 });
 Console.WriteLine(result1);

 //  Reverse  反转顺序    (影响原数组,没有返回值)

 string[] strings = { "a", "abc", "abcd", "ba" };
 Array.Reverse(strings);  
 foreach (var item in strings)
 {
     Console.WriteLine(item);
 }

 // 排序   默认按正序(从小到大  a-z)
 int[] i100 = { 7, 5, 8, 3, 6 };
 Array.Sort(i100);
 foreach (var item in i100)
 {
     Console.WriteLine(item);
 }

//Copy

string[] strings = { "a", "abc", "abcd", "ba" };

string[] strings3 = new string[3];
Array.Copy(strings, strings3, 2);//从左往右复制两个字符

//CopyTo 

string[] strings4 = new string[3];
strings3.CopyTo(strings4, 0);//从索引位置0开始将其后边的全部复制

//Clone 浅拷贝

string[] strings = { "a", "abc", "abcd", "ba" };

 object obj = strings.Clone();  // 把原来的数组先装箱object
 string[] strings2 = (string[])obj;  // 拆箱,拆箱产生一个新的数组
 strings2[0] = "aaa";

//Resize 将一维数组的元素数更改为指定的新大小。

string[] strings = { "a", "abc", "abcd", "ba" };

 Array.Resize(ref strings, strings.Length + 5);
 Console.WriteLine(strings.Length);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值