Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中的对象数组中获取最大值#_C# - Fatal编程技术网

C# 从C中的对象数组中获取最大值#

C# 从C中的对象数组中获取最大值#,c#,C#,我有一个包含对象的数组,这些对象有一个字符串、一个整数和一个字符属性 Person[] people = new Person[1]; //Declare the array of objects [...] This code is irrelevant Person person = new Person(name, age, gender); //This creates instance a new object people.SetValue(person, i); //is ju

我有一个包含对象的数组,这些对象有一个字符串、一个整数和一个字符属性

Person[] people = new Person[1]; //Declare the array of objects

[...] This code is irrelevant

Person person = new Person(name, age, gender); //This creates instance a new object
people.SetValue(person, i); //is just a variable which increase in a c
Array.Resize(ref people, people.Length + 1);  //Change array  size 
i++; // Autoincrement
[…]更多用于填充有效值的代码


从people数组中存储的所有对象person中,我想获得person的年龄最大值(年龄属性为整数值)

最简单的方法是使用LINQ:

using System.Linq;

var maxVal = people.Max(x => x.Age); // get highest age
var person = people.First(x => x.Age == maxVal); // get someone with such an age

我假设您的Person类如下所示:-

class Person
{
  public int Age { get; private set; }
  public string Name { get; private set; }
  public string Gender { get; private set; }

  // constructor etc
}
改进

如果您有一个可枚举的
Person
,即“people”,我建议您使用一个带有接口(如IList)的容器,即:-

IList<Person> people = new List<Person>();
进一步阅读

这是一个Linq查询;更多信息可在此处找到:-

使用linq可以做很多“酷”的事情,比如选择可枚举的年龄,例如

var ages = people.Select(p => p.Age);
这将返回一个包含列表中每个人的年龄的可枚举项。如果您想要所有超过一定年龄的人,可以使用
Where
,即:-

var above = people.Where(p => p.Age > 65);

如果每次添加新项时都要调整数组的大小,那么就不应该使用数组。这就是generic
List
类的用途。你可以写:

List<Person> People = new List<Person>();
Person person = new Person(name, age, gender);
People.Add(person);

我完全同意@dlev留下的评论,你不应该使用传统的,而应该使用自动调整收藏大小的

此外,由于和都实现了IEnumerable,您可以使用它们来操作元素,因此实际上,请查看可用的

中的特定示例适用于您的场景:

var maxVal = people.Max(x => x.Age);
var person = people.First(x => x.Age == maxVal);

像这样的事情会对你有帮助:

Person[] persons = {};
Person oldest = persons.Aggregate( (x,y) => x.Age > y.Age ? x : y ) ;
或者你自己的:

static T FindMax<T>( this IEnumerable<T> items , Func<T,T,int> comparer  ) where T:class
{
  T max = null ;
  foreach ( T item in items )
  {
    if ( item == null ) { continue ; }
    if ( max == null ) { max = item ; continue ; }

    // check current item against max.
    // if current item is "greater than" the current max
    // replace it.
    int cc = comparer(item,max) ;
    if ( cc > 0 )
    {
      max = item ;
    }
  }
  return max ;
}
static T FindMax(此IEnumerable items,Func comparer),其中T:class
{
T max=null;
foreach(项目中的T项目)
{
如果(item==null){continue;}
如果(max==null){max=item;continue;}
//对照最大值检查当前项目。
//如果当前项“大于”当前最大值
//替换它。
int cc=比较器(项目,最大值);
如果(cc>0)
{
最大值=项目;
}
}
返回最大值;
}
用法:

Person[] persons = LoadPersons() ;
Person oldest = persons.FindMax<Person>( (x,y) => x.Age < y.Age ? -1 : x.Age > y.Age ? +1 : 0 ) ;
Person[]persons=LoadPersons();
最老的人=人。FindMax((x,y)=>x.年龄y.年龄?+1:0);

如果列表相对较小(少于1000个对象),则上述建议将起作用。这是因为上述所有方法都使用顺序搜索。出于性能目的(速度),应用qsort方法更有效。例如:

class PersonAgeComparer : IComparer<Person>
{
    public Int32 Compare(Person x, Person y)
    {                
        if (x.Age > y.Age) return 1;
        if (x.Age < y.Age) return -1;

        return 0; // must be equal
    }
}

class Person
{
    public Int32 Age { get; set; }
}

static void Run1()
{
    Random rnd = new Random((Int32)DateTime.Now.Ticks);

    List<Person> persons = new List<Person>();
    for (Int32 i = 0; i < 100000; i++)
    {
        persons.Add(new Person() { Age = rnd.Next(100) });
    }

    persons.Sort(new PersonAgeComparer());

    // The oldest but you may have dups (same age)
    Person oldest = persons[persons.Count - 1];

}
class PersonAgeComparer:IComparer
{
公共Int32比较(人员x、人员y)
{                
如果(x.Age>y.Age)返回1;
如果(x.Age
最大值本身,还是具有该最大值的人?如果是前者,那么您可以只使用
people.Max(person=>person.Age)另外,不要像这样手动调整数组大小。使用一个
列表
,它将为您处理大小调整。“只是一个在c中增加的变量”什么是“a c”?这是linq的一个糟糕用法。你需要两次通过列表。你只需要做一个。
Person[] persons = LoadPersons() ;
Person oldest = persons.FindMax<Person>( (x,y) => x.Age < y.Age ? -1 : x.Age > y.Age ? +1 : 0 ) ;
class PersonAgeComparer : IComparer<Person>
{
    public Int32 Compare(Person x, Person y)
    {                
        if (x.Age > y.Age) return 1;
        if (x.Age < y.Age) return -1;

        return 0; // must be equal
    }
}

class Person
{
    public Int32 Age { get; set; }
}

static void Run1()
{
    Random rnd = new Random((Int32)DateTime.Now.Ticks);

    List<Person> persons = new List<Person>();
    for (Int32 i = 0; i < 100000; i++)
    {
        persons.Add(new Person() { Age = rnd.Next(100) });
    }

    persons.Sort(new PersonAgeComparer());

    // The oldest but you may have dups (same age)
    Person oldest = persons[persons.Count - 1];

}