C#中数组定义、查询、删除及相关操作

一维/二维数组:

 int[] myintArray = { 1, 2, 3 ,9,15,32,4};
 int[,] myintArray= { { 12, 34 }, { 56, 78 } };

数据升序/降序:

  int[] myintArray = { 1, 2, 3 ,9,15,32,4};
  Array.Sort(myintArray);
  foreach(int outint in myintArray)
  System.Diagnostics.Debug.WriteLine("升序"+"\r"+outint + "\r");
  Array.Reverse(myintArray);
  foreach (int outint in myintArray)
  System.Diagnostics.Debug.Write("降序" + "\r" + outint + "\r");

数组的合并:

 int[] myintArray = { 1, 2, 3 ,9,15,32,4};
 int[] myintArray2 = { 11, 22, 33, 99, 155, 322, 44 };
            

 foreach (int outint in myintArray)
 System.Diagnostics.Debug.WriteLine("原数组1为:" + outint + "\r");
 foreach (int outint in myintArray2)
 System.Diagnostics.Debug.WriteLine("原数组2为:" + outint+ "\r");

 int[] resultNum = new int[20];
 int mA_longth = myintArray.GetLength(0);//数组1长度
 int mA2_longth = myintArray2.GetLength(0);//数组2长度
 Array.Copy(myintArray, resultNum, mA_longth);
 Array.Copy(myintArray2, 0, resultNum, mA_longth, mA2_longth);
 foreach (int outint in resultNum)
 System.Diagnostics.Debug.WriteLine("使用第一种:" + outint + "\r");
            

ArrayList添加和删除数组内容:

            //数组添加
            ArrayList myArrayList = new ArrayList(5);
            myArrayList.Add(1);
            myArrayList.Add(2);
            myArrayList.Add(3);
            myArrayList.Add("A");
            myArrayList.Add("B");
            myArrayList.Add("C");
            System.Diagnostics.Debug.WriteLine("数组数量为{0}" , myArrayList.Count );
            string[] mystringArray = { "张三","李四","王五"};
            myArrayList.AddRange(mystringArray);
            System.Diagnostics.Debug.WriteLine("range数组数量为{0}" ,myArrayList.Count);
            foreach (object outarray in myArrayList) 
            System.Diagnostics.Debug.WriteLine("数组元素"+outarray+"\r");
            //数组删除
            myArrayList.Remove("A");
            foreach (object outarray in myArrayList) 
            System.Diagnostics.Debug.WriteLine("删除后" + outarray + "\r");
            myArrayList.RemoveAt(4);
            foreach (object outarray in myArrayList)
            System.Diagnostics.Debug.WriteLine("删除索引后" + outarray + "\r");
            myArrayList.RemoveRange(4, 3);
            foreach (object outarray in myArrayList)
            System.Diagnostics.Debug.WriteLine("删除区间索引后" + outarray + "\r");
            myArrayList.Clear();
            foreach (object outarray in myArrayList)
             System.Diagnostics.Debug.WriteLine("删除所有元素后" + outarray + "\r");

ArrayList数组查询:

            //数组添加
            ArrayList myArrayList = new ArrayList(5);
            myArrayList.Add(1);
            myArrayList.Add(2);
            myArrayList.Add(3);
            myArrayList.Add("A");
            myArrayList.Add("B");
            myArrayList.Add("C");
           //查找数据
            int indexResult= myArrayList.IndexOf("张三");
            foreach (object outarray in myArrayList)
           System.Diagnostics.Debug.WriteLine("查询结果{0}" ,indexResult );
            int indexResult_La = myArrayList.LastIndexOf("王五");
            foreach (object outarray in myArrayList)
             System.Diagnostics.Debug.WriteLine("查询结果{0}", indexResult_La);
            //int indexResult_LaB = myArrayList.BinarySearch("王五");//首次出现索引值
            //foreach (object outarray in myArrayList)
            // System.Diagnostics.Debug.WriteLine("查询结果{0}", indexResult_LaB);

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

品尚公益团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值