関数を追加および削除するリストを作成し、リストをスクリーニングして最大年齢を選択するメソッドを記述します

Program.csの

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

namespace diyConSole
{


    //public class Person
    //{

    //    private string name;
    //    private int age;

    //    public string Name
    //    {
    //        get
    //        {
    //            return name;
    //        }

    //        set
    //        {
    //            name = value;
    //        }
    //    }

    //    public int Age
    //    {
    //        get
    //        {
    //            return age;
    //        }

    //        set
    //        {
    //            age = value;
    //        }
    //    }


       

    //}
    class Program
    {


     //   public static void GetOldest(List<Person> _lstPerson)
     //   {




     //       //写一个方法对List<Person> 进行筛查 选出最大的age

     //       Person oldestPerson = null;

     //       List<Person> tempList = new List<Person>();

     //       Person currentPerson = new Person();

     //       foreach (Person p in _lstPerson)
     //       {

     //           currentPerson = p;

     //           if (oldestPerson == null)
     //           {
     //               oldestPerson = currentPerson;
     //               tempList.Clear();
     //               tempList.Add(oldestPerson);
     //           }
     //           else if (oldestPerson.Age >= currentPerson.Age)
     //           {
     //               tempList.Clear();
     //               tempList.Add(oldestPerson);

     //           }

     //           else if (currentPerson.Age >= oldestPerson.Age)
     //           {

     //               tempList.Clear();
     //               tempList.Add(currentPerson);

     //           }



     //       }


     //       Person[] newPerson = new Person[tempList.Count];

     //       int index = 0;

     //       foreach (Person ss in tempList)
     //       {
     //           newPerson[index] = ss;
     //           index++;

     //           Console.WriteLine(ss.Name);
     //       }

     ////       Console.WriteLine(newPerson[0].Name);


     //   }


        static void Main(string[] args)
        {


            List<Person> myPeople = new List<Person>();
           
            Person newPerson = new Person();
     
            Person OldDavid = new Person();
            
            

            OldDavid.Name = "OldDavid";
            OldDavid.Age = 66;


            Person LittleHerry = new Person();
            LittleHerry.Name = "LittleHerry";
            LittleHerry.Age = 33;


            myPeople.Add(OldDavid);
            myPeople.Add(LittleHerry);


           Person[] ss = LittleHerry.GetOldest(myPeople);

            //foreach (Person Family in myPeople)
            //{


            //    Console.WriteLine(Family.Name);

            //}

            Console.WriteLine(ss[0].Name);

            Console.ReadLine();

        }
    }
}

Person.cs

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

using System.Collections;

namespace Ch10
{
  public  class Person
    {

        private string name;
        private int age;

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }

            set
            {
                age = value;
            }
        }


        public Person[] GetOldest(List<Person> _lstPerson )
        {

            //写一个方法对List<Person> 进行筛查 选出最大的age

            Person oldestPerson = null;

            List<Person> tempList = new List<Person>();

            Person currentPerson = new Person();

            foreach (Person p in _lstPerson)
            {

                currentPerson = p;

                if (oldestPerson == null)
                {
                    oldestPerson = currentPerson;
                    tempList.Clear();
                    tempList.Add(oldestPerson);
                }
                else if (oldestPerson.age >= currentPerson.age)
                {
                    tempList.Clear();
                    tempList.Add(oldestPerson);

                }

                else if (currentPerson.age >= oldestPerson.age)
                {

                    tempList.Clear();
                    tempList.Add(currentPerson);

                }


              
            }


            Person[] newPerson = new Person[tempList.Count];

            int index = 0;

            foreach (Person ss in tempList)
            {
                newPerson[index] = ss;
                index++;


            }

            return newPerson;


        }

    }







}

元の記事を271件公開 44のように 50,000以上を訪問

おすすめ

転載: blog.csdn.net/qq_38992403/article/details/105426870