Unity C# 基础复习15——Array(P369)

简单研究Array

CreateArr()创建数组以及ForeachArr()遍历数组

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            ForeachArr(new int[] { 1, 2, 3 });
            ForeachArr(new String[] { "abc", "bcd" });
        }

        public static void ForeachArr(Array arr)
        {
            for (int i = 0 ; i < arr.Length; i++)
            {
                Console.WriteLine(arr.GetValue(i));
            }
        }
        public static void CreateArr()
        {
            //子类直接创建数组
            int[] arr;
            String[] arr2;

            //直接用Array的方式创建数组
            Array arr3 = Array.CreateInstance(typeof(String), 100);
            //String[] arr3 = new String[100]
            arr3.SetValue("abc", 0);
            Console.WriteLine(arr3.GetValue(0));
        }
    }
}

多维数组

public static void MutipleRanksArr()
        {
            //两种创建二维数组的方法
            //Array
            Array arrArr = Array.CreateInstance(typeof(int), 5, 5);
            Console.WriteLine(arrArr.GetValue(1, 1));
            //子类方式
            int[,] arrArr2 = new int[5, 5];
            Console.WriteLine(arrArr2[1, 1]);

            //多维遍历
            for (int i = 0; i < arrArr2.GetLength(0); i++)
            {
                for (int j = 0; j < arrArr2.GetLength(1); j++)
                {
                    Console.Write(arrArr2[i, j]);
                }
                Console.WriteLine();
            }
        }

1、设置一个数组只能读取,无法修正

static void Main(string[] args)
        {
            ReadOnlyCollection<int> read = ReadonlyArr(new int[] { 1, 2, 3 });
        }

        public static ReadOnlyCollection<int> ReadonlyArr(int[] arr)
        {
            ReadOnlyCollection<int> read = Array.AsReadOnly(arr);
            Console.WriteLine(read[0]);
            return read;
        }

2、为一个数组进行排序

//排序
        public static void SortArr()
        {
            String[] str = { "abc", "bcd", null, "123" };
            Array.Sort(str);
            ForeachArr(str);
        }

3、清空一个数组,为下一次填充做好准备

public static void Init(int[] arr, params int[] nums)
        {
            Array.Clear(arr, 0, arr.Length);
            //清空原集合 为填充做准备
            for (int i = 0; i < nums.Length; i++)
            {
                arr[i] = nums[i];
            }
        }

4、将一个数组的后一半合并到另一个数组中

 static void Main(string[] args)
        {
            Copy(new int[] { 1, 2, 3, 4, 5 }, new int[] { 6, 7, 8, 9, 10 });
        } 

public static void Copy(int[] src, int[] dest)
        {
            //为数组扩容
            int oldLen = dest.Length;
            Array.Resize(ref dest, dest.Length + (src.Length + 1) / 2);
            //进行复制
            Array.Copy(src, src.Length / 2, dest, oldLen, (src.Length + 1) / 2);
            ForeachArr(dest);
        }

5、判断两个数组是否相等

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(EqArr(new int[] { 1, 2 }, new int[] { 1, 2 }));
        }



public static bool EqArr(int[] arr1, int[] arr2)
        {
            //Console.WriteLine(arr1.Equals(arr2));   //Equals支持引用相等,堆中的地址相等,但是因为引用不相等导致堆开辟两空间,因此类库不支持

            if (arr1 == arr2)
            {
                return true;
            }

            else if (arr1 == null || arr2 == null)
            {
                return false;
            }
            else if (arr1.Length != arr2.Length)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < arr1.Length; i++)
                {
                    if (arr1[i] != arr2[i])
                    {
                        return false;
                    }
                }
                return true;
            }
        }

6、将数组反序

static void Main(string[] args)
        {
            
            ReverseArr(new int[] { 5, 4, 3, 2, 1 });
        }

public static void ReverseArr(int[] nums)
        {
            Array.Reverse(nums);
            ForeachArr(nums);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值