关于数组.

昨天作业

  1. 定义一个数组,再逆序存储它的值

    int[] a = new int[] { 1,2,3,4 };
    经过一系列操作后将数组变成: {4,3,2,1}

  2. 定义一个长度为 50 的数组,数组里面的值是从 0~99 随机产生的

    求出其中 30 出现的次数

  3. 古诗判断

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _01昨天作业
    {
        internal class Program
        {
            static void Main(string[] args)
            {
    
                int[] ints= {1, 2, 3 ,4,5,6};
                Reverse(ints);
            
    
                int[] ints1 = new int[50];
                Random random = new Random();
                for (int i = 0; i < ints1.Length; i++)
                {
                   // Console.WriteLine(random.Next(0,100));
                    ints1[i] = random.Next(20, 41);
                }
                //foreach (int v in ints1)
                //{
                //    Console.WriteLine(v);
                //}
                Console.WriteLine(FindNumCount(ints1,30));
    
                // 判断数组中40出现的次数
    
                Console.WriteLine(FindNumCount(ints1, 40));
    
                Console.WriteLine("请默写李白的《黄鹤楼送孟浩然之广陵》:");
    
                string[] poetry = new string[] { "故人西辞黄鹤楼", "烟花三月下扬州", "孤帆远影碧空尽", "惟见长江天际流" };
    
                for (int i = 0; i < poetry.Length; i++)
                {
                    // Console.Write("请默写第{0}句:", i + 1);
                    Console.Write($"请默写第{i+1}句:");
                    string sentence = Console.ReadLine(); //从控制台输入诗句
                    if (sentence == poetry[i]) //判断诗句输入是否正确
                        Console.WriteLine("第{0}句默写正确!", i + 1);
                    else
                        Console.WriteLine($"第{i+1}句默写错误!正确答案为:{poetry[i]}");
                }
    
    
    
            }
            static public void Reverse(int[] arr)
            {
                //逆序数组,只需要循环数组的一半,交换当前位置和后面对应的位置的数据即可
    
                for (int i = 0; i < arr.Length/2; i++)
                {
    
                    //i==0  
                    // (1,6) =(6,1)
                    //i==1
                    // (2,5) = (5,2)
    
                    (arr[i], arr[arr.Length - i - 1]) = (arr[arr.Length - i - 1], arr[i]);
    
                }
    
            }
            static public int FindNumCount(int[] arr,int value)
            {
                int count = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    if (arr[i]==value) { count++; }
    
                }
    
                return count;
            }
              
        }
    }
    
    

交错数组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _02交错数组
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //交错数组:就是装着数组的数组,交错数组存储的也是数组
            //交错数组和多维数组的区别在于,交错数组的每一行的长度可以不同
            int[,] ints = new int[,]
            {
                {1,2},
                {3,4}
            };

            //交错数组的定义
            int[] ints1; //一层交错数组,就是普通的一维数组
            int[][] ints2;//二层交错数组,数组中装一层交错数组
            int[][][] ints3;//三层交错数组,数组中装二层交错数组


            int[] ints4 = new int[] { 1, 2, 3 };    
            int[][] ints5 = new int[][]
            {
                new int[] { 1, 2},
                new int[] { 3, 4,3},
            };
            int[][][] ints6 = new int[][][]
            {
                new int[][]
                {
                    new int []{1,2,3},
                    new int []{1,2,3}
                },
                 new int[][]
                {
                    new int []{1,2,3,6,8}
                }
            };

            //注意:如果初始化的时候,没有指定初始值必须指定第一层的长度


            int[] ints7_1 = new int[] { 1, 2, 3, 4 };
            int[] ints7_2 = new int[] { 5, 6, 7 };
            int[] ints7_3 = new int[] { 8, 9, 10 };
            int[][] ints7 = new int[3][]
            {
                ints7_1,
                ints7_2,
                ints7_3,
            };

            //交错数组如何进行数组的操作   数组名[第一层数组索引][第二层数组的索引]
            Console.WriteLine(ints7[0][2]);

            //交错数组的Length返回的是这个数组本身的长度
            Console.WriteLine(ints7.Length);
            Console.WriteLine(ints7[1].Length);

            //交错数组其实就是数组里面存数组,可以使用foreach一层一层的循环
            foreach (int[] v in ints7)
            {
                //Console.WriteLine(v);
                foreach (int vv in v)
                {
                    Console.WriteLine(vv);
                }
            }

            //也可以使用for循环 一层一层循环
            for (int i = 0; i < ints7.Length; i++)
            {
                for (int j = 0; j < ints7[i].Length; j++)
                {
                    Console.WriteLine(ints7[i][j]);
                }
            }





        }
    }
}

数组的属性和方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03数组的属性和方法
{
    internal class Program
    {
        static void Main(string[] args)
        {


            //所以的数组都是Array类的实例,Array提供了一系列的属性和方法

            //属性
            int[] ints = { 1, 2, 3, 4, 5, 6, 7 };
            int[,,] ints1 =
            {
                {
                    { 1, 2, 3}
                },
                {
                    { 1, 2, 3}
                },

            };

            Console.WriteLine(ints.Length);//返回一个32位的整数,表示数组的长度
            Console.WriteLine(ints.LongLength);//返回一个64位的整数,表示数组的长度
            Console.WriteLine(ints.Rank);//返回数组的维度 几维的
            Console.WriteLine(ints1.Rank);

            Console.WriteLine("------------------------");
            //常用的方法,方法用于帮助我们操作数组


            //将数组中指定的数据恢复为对应类型的默认值
            //参数1:需要恢复的数组,参数二:开始的索引 参数3:恢复的个数
            Array.Clear(ints, 1, 3);

            WriteArr(ints);//[1,0,0,0,5,6,7,]

            //将参数1数组中数据复制到参数2数组中,由参数3指定复制的个数
            //注意:1.复制的个数不能超过源数组的长度
            // 2.复制的个数不能超过新数组的长度
            int[] ints2 = new int[10];
            Array.Copy(ints, ints2, 6); // [1,0,0,0,5,6,0,0,0,0,]
            WriteArr(ints2);

            //复制的是数组中的数据,这是两个不同的数组,因为不会互相影响
            ints[0] = 100;
            Console.WriteLine(ints2[0]);

            //-----------------------
            People[] people1 =
            {
                new People (){Name="吴亦凡"}
            };

            People[] people2 = new People[2];
            Array.Copy(people1, people2, people1.Length);

            people2[0].Name = "罗志祥";
            Console.WriteLine(people1[0].Name);//罗志祥

            people2[0] = new People() { Name = "权志龙" };
            Console.WriteLine(people1[0].Name);//罗志祥

            //----------------

            int[] ints3 = new int[] { 2, 4, 5, 6, 7, 8, 9 };
            int[] ints4 = new int[6];
            /*
             * 参数1:被复制的数组
             * 参数2:复制开始的位置
             * 参数3:复制到的目标数组
             * 参数4:目标数组的开始位置
             * 参数5:复制的个数
             * 从ints3的位置2开始复制到ints4中,从ints4的位置1开始放,复制4个
             */
            Array.Copy (ints3,2, ints4, 1,4);
            WriteArr(ints4);//[0,5,6,7,8,0,]


            //反转数组
            Array.Reverse(ints4);
            WriteArr(ints4);//[0,8,7,6,5,0,]

            //查找对应数据在数组中第一次出现的位置,有则返回当前数据的索引,没有则返回-1
            Console.WriteLine(Array.IndexOf(ints4,99));
            //参数3是指定开始查询的位置
            Console.WriteLine(Array.IndexOf(ints4, 7,3));
            // 参数4指定查询的个数,从ints4中查询0 从2开始 查询3个
            Console.WriteLine(Array.IndexOf(ints4, 0,2, 3));

            //----------------------------------
            int[] ages = { 28, 32, 31, 16, 24, 13 };

            //需求:查询数组第一个年龄为18的索引  indexOf();
            //如果需要对数组中的数据进行范围查询,indexOf不能满足需求
            //如:查询数组中第一个不满18的数字  

            //根据传入的数组和方法,返回第一个满足条件的数据
            //Find的工作:循环该数组,并且每次循环调用传递的方法,并且将当前的数据通过参数传递到方法中,如果方法返回了ture,则Find直接返回当前这个参数
            //参数1:查询的数组
            //参数2:一个方法,这个方法应该返回一个布尔值,表示当前数值是否满足条件
            Console.WriteLine(Array.Find(ages, FindSmall18));
            //需求:返回数组中第一个奇数
            Console.WriteLine(Array.Find(ages, FindOdd));

            //需求:查询能同时被3和5整除的数字(如果数组中没有满足条件的数据,则返回该类型的默认值)
            Console.WriteLine(Array.Find(ages, Fn1));

            string[] strings = { "123123", "123123", "aa1s", "123edsa" };

            //因为没有字符串满足条件,因此返回null
            Console.WriteLine(Array.Find(strings, Fn2));//null
            string v = Array.Find(strings, Fn2);
            Console.WriteLine(string.IsNullOrEmpty(v));//true

            // FindIndex用法和Find相同,FindIndex返回第一个满足条件的数据的索引,都不满足则返回-1
            Console.WriteLine(Array.FindIndex(ages, FindSmall18));//3
            Console.WriteLine(Array.FindIndex(ages, FindOdd));//2
            Console.WriteLine(Array.FindIndex(ages, Fn1));//-1
            Console.WriteLine(Array.FindIndex(strings, Fn2));//-1

            //从后往前查询第一个满足条件的数据
            Console.WriteLine(Array.FindLast(ages, FindSmall18));//13
            //从后往前查询第一个满足条件的数据的索引
            Console.WriteLine(Array.FindLastIndex(ages, FindSmall18));//5

            //查询所有满足条件的数据
            int[] ages1=  Array.FindAll(ages, FindSmall18);
            WriteArr(ages1);

            // Console.WriteLine(ages1);
            int[] ints6 = { 2, 4, 6 };
            //判断数组中是否所有的数据都满足条件(所有的数据都满足条件就返回true 有一个不满足就返回false)
            Console.WriteLine(Array.TrueForAll(ints6,Fn3));//True
            ints6[2] = 3;
            Console.WriteLine(Array.TrueForAll(ints6, Fn3));//False

            //判断数组中是否至少有一个满足条件(一个满足就返回true,所有的都不满足就返回false)
            Console.WriteLine(Array.Exists(ints6, Fn3));//True
            ints6[0] = 1;
            ints6[1] = 1;
            Console.WriteLine(Array.Exists(ints6, Fn3));//False

            //用于循环数组的方法
            Array.ForEach(ints6, Fn4);

            //------------------------------------------------------------------------------

            //之前这几个方法都是Array类上的方法,需要使用Array.xxx调用
            //以下几个方法都是Array实例上的方法, 数组.xxx调用

            string[] strs = new string[] { "吴亦凡", "罗志祥", "李云迪" };
            string[] strs1 = new string[5];
            //将strs复制到strs1中,从strs1的位置1开始存放
            strs.CopyTo(strs1, 1);
            WriteArr1(strs1);// [null, 吴亦凡, 罗志祥, 李云迪,null]


            int[,,] ints8 = new int[10, 3, 4];

            //获取指定维度的数据的数量 返回的是一个32位的整数
            Console.WriteLine(strs.GetLength(0));//3
            Console.WriteLine(ints8.GetLength(0));//10
            Console.WriteLine(ints8.GetLength(2));//4
            //获取指定维度的数据的数量 返回的是一个64位的整数
            Console.WriteLine(strs.GetLongLength(0));//3
            Console.WriteLine(ints8.GetLongLength(0));//10
            Console.WriteLine(ints8.GetLongLength(2));//4

            //等同于 strs[1] ="权志龙"
            strs.SetValue("权志龙", 1);
            WriteArr1(strs);
            //获取数组中指定索引位置的数据 等同于 strs[2]
            Console.WriteLine(strs.GetValue(2));

            //可以给他传入多个索引进行多维数据的操作
            //设置ints1[5,2,3}的值为666 等同于 ints8[5,2,3]=666
            ints8.SetValue(666, 5, 2, 3);
            //读取多维数组
            Console.WriteLine(ints8.GetValue(5,2,3));
            Console.WriteLine(ints8[5, 2, 3]);


            //判断数组中是否存在某个数据
            Console.WriteLine(strs.Contains("吴亦凡"));

            //strs.All() 等同于 Array.TrueForAll()
            //strs.Any() 等同于 Array.Exists()
        }
        static public string WriteArr(int[] arr)
        {
            string s = "[";
            for (int i = 0; i < arr.Length; i++)
            {
                s += arr[i] + ",";
            }
            s += "]";
            Console.WriteLine(s);
            return s;
             
        }
        static public string WriteArr1(string[] arr)
        {
            string s = "[";
            for (int i = 0; i < arr.Length; i++)
            {
                s += arr[i] + ",";
            }
            s += "]";
            Console.WriteLine(s);
            return s;

        }




        static public bool FindSmall18(int value)
        {
            //Console.WriteLine("value=="+value);
            //if (value<18)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}

            // 因为逻辑运算得到的结果就是布尔值,因为可以简化为:
            return value < 18;
        }
        static public bool FindOdd(int value)
        {
            return value % 2 != 0;
        }
        static public bool Fn1(int value)
        {
            return value%3 == 0 && value% 5==0;
        }
        static public bool Fn2(string value)
        {
            return value.Length == 3;
        }
        static public bool Fn3(int value)
        {
            return value % 2 == 0;
        }
        static public void Fn4(int value)
        {
            Console.WriteLine(value);
        }
    }
    class People
    {
        public string Name { get; set; }
    }
}

练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04练习
{
    internal class Program
    {
        static void Main(string[] args)
        {
            People[] peoples =
            {
                new People {Name="吴亦凡",Age=18,Sex=People.ESex.man},
                new People {Name="郑爽",Age=22,Sex=People.ESex.woman},
                new People {Name="李云迪",Age=21,Sex=People.ESex.man},
                new People {Name="蔡徐坤",Age=32,Sex=People.ESex.man},
                new People {Name="权志龙",Age=8,Sex=People.ESex.man},
                new People {Name="杨幂",Age=18,Sex=People.ESex.woman}
            };

            //1.查询peoples中所有的男性
            People[] ps1= Array.FindAll(peoples, FindAllMan);
            Console.WriteLine(ps1.Length);
            //简化写法
            People[] ps2 = Array.FindAll(peoples, p => p.Sex == People.ESex.man);
            Console.WriteLine(ps2.Length);

      
            //2.查询peoples中第一个女性
            People  p3 = Array.Find(peoples, FindFirstWoman);
            People  p4 = Array.Find(peoples, p=> p.Sex == People.ESex.woman);

            //3.判断数组中是否全部都是成年人

            Console.WriteLine(Array.TrueForAll(peoples,Find18));
            Console.WriteLine(Array.TrueForAll(peoples, p=>p.Age>=18));

            //4.计算所有人的年龄的平均值
            int allAge = 0;
            foreach (People people in peoples)
            {
                allAge += people.Age;
            }
            Console.WriteLine("平均值:"+(double)allAge/peoples.Length);

            int[] ints = new int[] { 1, 2, 10 };

            //计算数组的平均值
            Console.WriteLine(ints.Average());
            // People不能直接计算,需要传递一个转换方法,吧People变成数字让他计算
            Console.WriteLine(peoples.Average(PeopleToInt));
            Console.WriteLine(peoples.Average(p=>p.Age));
            //5.查询所有人中第一个未成年的男性

            People child=Array.Find(peoples,FindChild);
            People child2 = Array.Find(peoples, p=> p.Age < 18 && p.Sex == People.ESex.man);


        }
        static public bool FindAllMan(People p)
        {
            return p.Sex==People.ESex.man;
        }
        static public bool FindFirstWoman(People p)
        {
            return p.Sex == People.ESex.woman;
        }
        static public bool Find18(People p)
        {
            return p.Age >= 18;
        }
        // 转换方法, 将People转成People.Age
        static public int PeopleToInt(People p)
        {
            return p.Age;
        }
        static public bool FindChild(People p)
        {
            return p.Age < 18 && p.Sex == People.ESex.man;
        }
    }
    class People
    {
        public enum ESex
        {
            man,
            woman
        }
        public string Name { get; set; }
        public int Age { get; set; }
        public ESex Sex { get; set; }
    }

}

作业

  1. 动态生成(用循环生成)一个交错数组

    内容第一个数组:0 第二个数组:1 2 第三个数组 3 4 5 第四个数组 6 7 8 9
    [
    [0],
    [1,2],
    [3,4,5],
    [6,7,8,9]
    ]

  2. 实现一个方法用于打乱一个数组

  3. 将数组中的数据排序

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值